Cannot find module in Nodejs

醉酒当歌 提交于 2019-12-30 00:54:33

问题


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.


回答1:


In ./models/todo, the period indicates that node will look in the same folder that api.js is 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:

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



回答2:


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

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




回答3:


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')



来源:https://stackoverflow.com/questions/20446514/cannot-find-module-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!