简体   繁体   中英

node cannot find express module?

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

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

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

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 running npm list -g .

Please check the following if you have installed express globally

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM