强制要求检查模块的当前目录
Force require to check the current directory for a module
我正在使用 Node.js 创建一个命令行应用程序。如何让 require
检查模块的当前目录?
比如我要运行测试目录下的命令:
root@hello:~/projects/testing$ mycmd
并获取该目录 node_modules/
中的模块,而不是使用全局版本。
var myModule = require("testing/mymodule");
使用 process.cwd()
找到它们的当前目录,然后我可以从 node_modules/
文件夹中获取依赖项。
/usr/local/lib/node_modules/mycmd/index.js:
require(process.cwd() + "/node_modules/request");
我正在使用 Node.js 创建一个命令行应用程序。如何让 require
检查模块的当前目录?
比如我要运行测试目录下的命令:
root@hello:~/projects/testing$ mycmd
并获取该目录 node_modules/
中的模块,而不是使用全局版本。
var myModule = require("testing/mymodule");
使用 process.cwd()
找到它们的当前目录,然后我可以从 node_modules/
文件夹中获取依赖项。
/usr/local/lib/node_modules/mycmd/index.js:
require(process.cwd() + "/node_modules/request");