Node.js study notes - problems encountered in installing the express module

When learning node.js and using the express framework to learn, many people will encounter:

Error: Cannot find module ‘express’
Require stack:

  • D:\vscode project\test\express\index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object. (D:\vscode project\test\express\index.js:1:15)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions…js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
    code: ‘MODULE_NOT_FOUND’,
    requireStack: [ ‘D:\vscode project\test\express\index.js’ ]
    }

Use the following method, the pro-test is effective! ! !

1. Install the express framework globally, open the command line with cmd, and enter the following command:

    npm install -g express

When detecting express: express --version
prompt: 'express' is not recognized as an internal or external command, operable program or batch file.
Note that if npm does not specify a version, the latest version is loaded by default, which is already a version above 4.xx.

Because the command tool is separated in the latest express 4.x version, you need to install a command tool and execute the command:

    npm install -g express-generator

 输入express --version验证

Effect display: (display version description is successful)
insert image description here

2. If the error Error: Cannot find module express is still reported when executing the js file.

Solution:
Execute again in your own project directory:
npm install express

Guess you like

Origin blog.csdn.net/qq_46304554/article/details/122571985