node cannot find express module?

11,677

Solution 1

You need to have express installed locally in your node_modules folder. Check your node_modules and install it again if it is not there

Solution 2

npm express -v doesn't show the version of Express that you have installed, it shows the version of npm.

To show which version of Express you have installed, try this:

npm ls express

Solution 3

Maybe it is not your problem, but it is often confusing distinction between global and local installations for beginners. If you use require('module_name') in your code, each module must be installed locally using npm install module_name or using dependencies in package.json.

Global installation is only for running commands like express my_new_project.

You can list all installed local modules for actual directory using npm list and all global ones runningnpm list -g.

Solution 4

Please check the following if you have installed express globally

i.e using the command npm install -g express

  1. Check if express module is imported in your code using

    var express = require('express')

  2. On windows check if the path C:\Users\<user_name>\AppData\Roaming\npm is on the PATH environment variable

For installations that do not have express installed globally

  1. Navigate to you folder C:\node project
  2. Do a local installation of Express using npm install express
  3. Import express module in your code using var express = require('express')
Share:
11,677

Related videos on Youtube

I'm Poor
Author by

I'm Poor

Updated on September 14, 2022

Comments

  • I'm Poor
    I'm Poor over 1 year

    I am unaware what is causing this problem, because I thought it should be working. I do have expressed installed, at least according to npm:

    C:\Users\phucker\Desktop>node init.js
    
    module.js:340
        throw err;
              ^
    Error: Cannot find module 'express'
        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\phucker\Desktop\init.js:1:77)
        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 Function.Module.runMain (module.js:497:10)
    
    C:\Users\phucker\Desktop>npm express -v
    1.3.2
    
    C:\Users\phucker\Desktop>
    

    the code contained in init.js is

    var express = require('express');
    var app = express();
    
    app.get('/', function(req, res){
      res.send('hello world');
    });
    
    app.listen(3000);
    
  • Admin
    Admin over 9 years
    Why? What is the point of being able to install something globally if it can't be used "globally"?
  • Akshat Jiwan Sharma
    Akshat Jiwan Sharma over 9 years
    @Sosukodo a very good explanation for this is on the npm blog global installations are good if you want to use the executable from your shell. For using them as a library in your program it is preferable to install them locally. Though you can create symbolic links from global installations as well.