NodeJS 需要另一个模块内的模块

eSi*_*oll 5 module node.js

我如何需要一个依赖于另一个模块的模块?例如:

Project/foo.js
Project/nodule_modules/MyModule/node_modules/AnotherModule //dependencie of MyModule
Run Code Online (Sandbox Code Playgroud)

如何从 foo.js 文件中要求“AnotherModule”?

Dan*_* D. 3

在模块中使用foo.js

var that = require('./node_modules/MyModule/node_modules/AnotherModule');
Run Code Online (Sandbox Code Playgroud)

请参阅http://nodejs.org/api/modules.html#modules_file_modules

  • var that = require('AnotherModule'); 如果没有传递前缀(/、./、../),则会自动调用 node_modules 文件夹中的任何模块 (2认同)