简体   繁体   English

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

[英]node.js local modules: module not found error in subfolder

Here is the folder structure of my application这是我的应用程序的文件夹结构

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

I am refering app_modules/bar and app_modules/foo as local modules我将app_modules/barapp_modules/foo称为本地模块

package.json Root folder 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"
  }

when i require the modules as当我需要模块时

var fooModule = require('app_modules/foo'); i got module not found error我有模块未找到错误

package.json for foo module foo模块的package.json

{
  "name": "foo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

By default, require looks for modules inside the node_modules folder.默认情况下, requirenode_modules文件夹中查找模块。 To include modules from a custom locations, you need to prefix the path with ./ or ../ depending the level the module is from the file from where you require it.要包含来自自定义位置的模块,您需要使用./../作为路径前缀,具体取决于模块来自您需要它的文件的级别。

So, If you want to require app_modules/foo from app.js , you will have to do:所以,如果你想从app.js要求app_modules/foo ,你必须这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM