node.js本地模块:子文件夹中找不到模块错误

时间:2018-02-28 05:32:59

标签: javascript node.js express npm

以下是我的应用程序的文件夹结构

Here is the folder structure of my application

我将app_modules/barapp_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"
}

1 个答案:

答案 0 :(得分:1)

默认情况下,require会在node_modules文件夹中查找模块。要包含自定义位置的模块,您需要在路径前加./../,具体取决于模块所在的文件级别。

因此,如果您想要app_modules/foo app.js,则需要执行以下操作:

var fooModule = require('./app_modules/foo');