在 Nodejs 中找不到模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20446514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:28:46  来源:igfitidea点击:

Cannot find module in Nodejs

node.jsmongodbcrud

提问by user2993058

module.js:340
    throw err;
          ^
Error: Cannot find module './models/todo'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\Users\Basel\Desktop\Todo List\routes\api.js:1:74)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

C:\Users\Basel\Desktop\Todo List>

Why this application won't start up? I've already tried a global npm install.

为什么这个应用程序无法启动?我已经尝试过全局 npm 安装。

回答by matth

In ./models/todo, the period indicates that node will look in the samefolder that api.jsis in, which would look for \Todo List\routes\models\todo.js. This does not start from the root of the application. To require this, you'll need to us two periods to jump up a level, and specify the app path as well:

在 中./models/todo,句点表示节点将在所在的同一文件夹中查找,该文件夹api.js将查找\Todo List\routes\models\todo.js. 这不是从应用程序的根开始。要实现这一点,您需要给我们两个时间段来跳转一个级别,并指定应用程序路径:

var todo = require('../app/models/todo');

回答by Chopper Lee

maybe you did not set the system value : NODE_PATH; it should point to your global module location;

也许你没有设置系统值:NODE_PATH; 它应该指向您的全局模块位置;

in Linux: export NODE_PATH=/usr/local/lib/node_modules/works good for me;

在 Linux 中:export NODE_PATH=/usr/local/lib/node_modules/对我很有用;

回答by aditya

in my case, the file name i had given in my require statement was wrong. I had my models file named posts.js and i was using require('./models/post'). It worked after i changed it to require ('.models/posts')

就我而言,我在 require 语句中给出的文件名是错误的。我的模型文件名为 posts.js,我使用的是 require('./models/post')。在我将其更改为 require ('.models/posts') 后它起作用了