Throw the error: Cannot find module 'express' in NodeJs

Dung Do Tien Sep 27 2021 1017

Hi, this is the first time I try to create an app with NodeJs. Like this:

 var express = require("express");
var app = express();
app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    return next();

It feels simple but when run the code above and I got an error : Error: Cannot find module 'express'.

 Error: Cannot find module 'express'
Require stack:
- /home/runner/4renqlovfw5/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at /home/runner/4renqlovfw5/index.js:1:15
    at Script.runInContext (vm.js:130:18)
    at Object.<anonymous> (/run_dir/interp.js:209:20)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)

I using Nodejs v14.17.5

Thanks for any suggestions.

Have 3 answer(s) found.
  • j

    jrobertojrmx Jose Roberto Sep 27 2021

    You need to check you install express or not. You can run the command below to help install or reinstall express for you.

     npm install express --save

    Or you can run npm install command to help update/install all package

  • M

    Mario Guzmán Sep 27 2021

    You need to install Express locally into the context of your application (node_modules folder):

     npm install express

    And re-try run your app.

  • I

    Irving Sep 27 2021

    The first time gets code from git to the local computer you need to run npm install to help update all dependencies packages.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close