6

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
  • 14,612
  • 20
  • 74
  • 130

3 Answers3

19

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
  • 14,612
  • 20
  • 74
  • 130
1

You can just use the lines given below

const path=require('path');
module.exports=path.dirname(require.main.filename);
gurkan
  • 884
  • 4
  • 16
  • 25
D. D
  • 31
  • 1
  • 3
1

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);