Cannot find module in Nodejs

65,603

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

Solution 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;

Share:
65,603

Related videos on Youtube

user2993058
Author by

user2993058

Updated on July 09, 2022

Comments

  • user2993058
    user2993058 almost 2 years
    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.

    • JohnnyHK
      JohnnyHK over 10 years
      You need to provide more details. Do you have a todo.js file in the models directory that's at the same level as the file that's calling require?
    • matth
      matth over 10 years
      So is it in the location \Todo List\app\models\todo.js?
  • Suhayb
    Suhayb almost 7 years
    in my case node parses ../ as it is in var todo = require('../app/models/todo'); so output would be ../app/models/todo not parent/app/models/todo!

Related