Get node.js installation path from code

I am trying to write a debug frame for node.js , and it is difficult for me to determine how to get the full path to the main module file, for example fs.js

I heard it in the /lib folder of the node installation, but I would need to get this from the code in a consistent way for various installation situations (including windows).

I tried to look at the values ​​of process and process.env for something like the node installation path, but I don’t see anything that immediately appears in me.

+6
source share
2 answers

To find where the installed node executable is located, you can look at process.execPath , which gives the absolute path to the node executable.

To find where a particular module is located, you can use require.resolve (module);

However, I think that global modules cannot be directly accessed from the file system as other regular modules, since they seem to be cached somewhere inside the executable.

+12
source

1- create a file in the root of the project, call its settings.js

2- add this code inside this file

 module.exports = { POST_MAX_SIZE : 40 , //MB UPLOAD_MAX_FILE_SIZE: 40, //MB PROJECT_DIR : __dirname }; 

3- and anytime you want your project directory to just use

 var settings = require("../settings.js"); settings.PROJECT_DIR; 

this way you will have all the project directories regarding this file;)

+1
source

Source: https://habr.com/ru/post/926876/


All Articles