以下是我的应用程序的文件夹结构
我将app_modules/bar
和app_modules/foo
称为本地模块
package.json 根文件夹
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"serve-favicon": "~2.4.5",
"foo": "file:app_modules/foo",
"bar": "file:app_modules/bar",
"hello": "file:hello"
}
当我需要模块
时 var fooModule = require('app_modules/foo');
我找不到模块错误
package.json
模块的 foo
{
"name": "foo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
答案 0 :(得分:1)
默认情况下,require
会在node_modules
文件夹中查找模块。要包含自定义位置的模块,您需要在路径前加./
或../
,具体取决于模块所在的文件级别。
因此,如果您想要app_modules/foo
app.js
,则需要执行以下操作:
var fooModule = require('./app_modules/foo');