從代碼中獲取node.js安裝路徑

[英]Get the node.js install path from code


I'm trying to write a debugging framework for node.js and am having a hard time figuring out how to get the full path to a core module file like fs.js.

我正在嘗試為node.js編寫調試框架,並且很難弄清楚如何獲得像fs.js這樣的核心模塊文件的完整路徑。

I've heard it's in the /lib folder of the node installation, but I'd need to get this from the code in a consistent way for a variety of install situations (including windows).

我聽說它位於節點安裝的/ lib文件夾中,但是我需要以一致的方式從代碼中獲取這些內容以適應各種安裝情況(包括windows)。

I've tried taking a look at the process and process.env values for something like a node install path but can't see anything that immediately pops out at me.

我已經嘗試過看一下像節點安裝路徑這樣的進程和process.env值,但看不到任何立即彈出的東西。

2 个解决方案

#1


13  

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

要查找已安裝的節點可執行文件所在的位置,可以查看process.execPath,它提供了Node可執行文件的絕對路徑。

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

要查找某個模塊的位置,可以使用require.resolve(module);

However I think the global modules are not directly accessible from the filesystem as other regular modules since they seem to be cached somewhere within the executable.

但是我認為全局模塊不能像其他常規模塊那樣直接從文件系統訪問,因為它們似乎被緩存在可執行文件的某個地方。

#2


1  

1- create a file in the project root call it settings.js

1-在項目根目錄中創建一個文件,調用它settings.js

2- inside this file add this code

2-在此文件中添加此代碼

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

3- and any time you want your project directory just use

3-以及您希望項目目錄使用的任何時間

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

in this way you will have all project directories relative to this file ;)

通過這種方式,您將擁有相對於此文件的所有項目目錄;)


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/03/7255d38d985d356764b2c68033cbdbd1.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com