17
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.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
user2993058
  • 238
  • 1
  • 2
  • 9

3 Answers3

31

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');
matth
  • 6,112
  • 4
  • 37
  • 43
  • 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! – Suhayb Jul 15 '17 at 15:23
14

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;

Chopper Lee
  • 1,557
  • 2
  • 11
  • 30
-1

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

aditya
  • 343
  • 2
  • 14