node.js本地模块:子文件夹中未找到模块错误 [英] node.js local modules: module not found error in subfolder

查看:233
本文介绍了node.js本地模块:子文件夹中未找到模块错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"
}

推荐答案

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

By default, require looks for modules inside the node_modules folder. 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.

因此,如果要从app.js中要求app_modules/foo,则必须执行以下操作:

So, If you want to require app_modules/foo from app.js, you will have to do:

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

这篇关于node.js本地模块:子文件夹中未找到模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆