4

I'm trying to use mainModule like this:

const { mainModule } = require('process');
module.exports = path.dirname(mainModule.filename);

But I'm receiving the following messages:

const mainModule: NodeJS.Module 'mainModule' is deprecatedts(6385)

Auto import from 'process' (property) NodeJS.Process.mainModule?: NodeJS.Module

@deprecated — since v14.0.0 - use require.main instead.

How can I Solve this?

Ricardo Rocha
  • 11,592
  • 13
  • 62
  • 108

3 Answers3

12

I found here that you just need to change this:

const { mainModule } = require('process');
module.exports = path.dirname(mainModule);

To this:

module.exports = path.dirname(require.main.filename);
Ricardo Rocha
  • 11,592
  • 13
  • 62
  • 108
1

You can just use the lines given below

const path=require('path');
module.exports=path.dirname(require.main.filename);
gurkan
  • 802
  • 4
  • 14
  • 24
D. D
  • 31
  • 1
  • 3
0

Since v14.0.0, mainModule is deprecated. Now, you can achieve the same thing just by writing the following lines:

const path = require('path');
module.exports = path.dirname(require.main.filename);