简体   繁体   English

Node.js 错误:找不到 express 模块

[英]Node.js Error: Cannot find module express

I wrote my first node.js app, but it can't find express library:我写了我的第一个 node.js 应用程序,但它找不到 express 库:

C:\ChatServer\Server>node server.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:\ChatServer\Server\server.js:6:9)
    at Object.<anonymous> (C:\ChatServer\Server\server.js:25:4)
    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)

But express is intalled (with -g key):但是 express 已安装(使用-g键):

C:\ChatServer\Server>npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/connect/2.7.11
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.4
npm http GET https://registry.npmjs.org/cookie/0.1.0
npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET https://registry.npmjs.org/fresh/0.1.0
npm http GET https://registry.npmjs.org/methods/0.0.1
npm http GET https://registry.npmjs.org/send/0.1.0
npm http GET https://registry.npmjs.org/cookie-signature/1.0.1
npm http GET https://registry.npmjs.org/debug
npm http 304 https://registry.npmjs.org/mkdirp/0.3.4
npm http 304 https://registry.npmjs.org/range-parser/0.0.4
npm http 304 https://registry.npmjs.org/cookie/0.1.0
npm http 304 https://registry.npmjs.org/connect/2.7.11
npm http 304 https://registry.npmjs.org/commander/0.6.1
npm WARN package.json range-parser@0.0.4 No repository field.
npm http 304 https://registry.npmjs.org/buffer-crc32/0.2.1
npm http 304 https://registry.npmjs.org/fresh/0.1.0
npm http 304 https://registry.npmjs.org/methods/0.0.1
npm http 304 https://registry.npmjs.org/send/0.1.0
npm http 304 https://registry.npmjs.org/cookie-signature/1.0.1
npm WARN package.json methods@0.0.1 No repository field.
npm WARN package.json methods@0.0.1 No readme data.
npm WARN package.json fresh@0.1.0 No repository field.
npm WARN package.json send@0.1.0 No repository field.
npm WARN package.json cookie-signature@1.0.1 No repository field.
npm http 304 https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/mime/1.2.6
npm http GET https://registry.npmjs.org/qs/0.6.5
npm http GET https://registry.npmjs.org/send/0.1.1
npm http GET https://registry.npmjs.org/formidable/1.0.14
npm http GET https://registry.npmjs.org/cookie/0.0.5
npm http GET https://registry.npmjs.org/bytes/0.2.0
npm http GET https://registry.npmjs.org/pause/0.0.1
npm http 304 https://registry.npmjs.org/mime/1.2.6
npm http 304 https://registry.npmjs.org/bytes/0.2.0
npm WARN package.json bytes@0.2.0 No repository field.
npm http 304 https://registry.npmjs.org/pause/0.0.1
npm WARN package.json pause@0.0.1 No repository field.
npm http 304 https://registry.npmjs.org/formidable/1.0.14
npm http 304 https://registry.npmjs.org/qs/0.6.5
npm http 304 https://registry.npmjs.org/send/0.1.1
npm http 304 https://registry.npmjs.org/cookie/0.0.5
npm http GET https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/mime
C:\Users\Dmitry\AppData\Roaming\npm\express -> C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express\bin\express
npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json assert-plus@0.1.2 No repository field.
npm WARN package.json ctype@0.5.2 No repository field.
express@3.2.6 C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express
├── methods@0.0.1
├── fresh@0.1.0
├── range-parser@0.0.4
├── cookie-signature@1.0.1
├── buffer-crc32@0.2.1
├── cookie@0.1.0
├── debug@0.7.2
├── commander@0.6.1
├── mkdirp@0.3.4
├── send@0.1.0 (mime@1.2.6)
└── connect@2.7.11 (pause@0.0.1, qs@0.6.5, bytes@0.2.0, cookie@0.0.5, formidable@1.0.14, send@0.1.1)

Some information:一些信息:

C:\ChatServer\Server>npm --version
1.2.24

C:\ChatServer\Server>node --version
v0.10.9

C:\ChatServer\Server>npm --version
1.2.24

C:\ChatServer\Server>npm ls -g installed express
npm WARN package.json cookie-signature@1.0.1 No repository field.
npm WARN package.json fresh@0.1.0 No repository field.
npm WARN package.json methods@0.0.1 No repository field.
npm WARN package.json range-parser@0.0.4 No repository field.
npm WARN package.json send@0.1.0 No repository field.
npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json bytes@0.2.0 No repository field.
npm WARN package.json pause@0.0.1 No repository field.
npm WARN package.json assert-plus@0.1.2 No repository field.
npm WARN package.json ctype@0.5.2 No repository field.
C:\Users\Dmitry\AppData\Roaming\npm
└── express@3.2.6


C:\ChatServer\Server>npm ls installed express
C:\ChatServer\Server
└── (empty)

How can I solve this problem?我怎么解决这个问题? ( MUST I install it without -g ?) (我必须在没有-g情况下安装它吗?)

You need to install Express locally into the context of your application ( node_modules folder):您需要将 Express 本地安装到应用程序的上下文中( node_modules文件夹):

$ npm install express

The reason for this is that applications always look in their local context for any dependencies.这样做的原因是应用程序总是在其本地上下文中查找任何依赖项。 The global installation is only for setting up system-wide available binaries, such as unit test runners or bootstrappers or things like that.全局安装仅用于设置系统范围内可用的二进制文件,例如单元测试运行程序或引导程序或类似的东西。

With Express, when you install it globally, you get an express binary that can bootstrap an application for you.使用 Express,当您全局安装它时,您将获得一个可以为您引导应用程序的express二进制文件。 For more information, type有关更多信息,请键入

$ express --help

So, to answer your final question: YES , you need to install it without -g .因此,要回答您的最后一个问题: YES ,您需要在没有-g情况下安装它。

对我来说,它在使用 --save 选项在本地安装 express 时起作用,如下所示:

$ npm install express --save

Check if you are not install express module, use this command:检查您是否没有安装express模块,使用以下命令:

 npm install express

and if your node_modules directory is in another place, set NODE_PATH envirnment variable:如果您的node_modules目录在另一个地方,请设置NODE_PATH环境变量:

 set NODE_PATH=your\directory\to\node_modules;%NODE_PATH%

Golo have explain well the solution, but I might add a clarification: Golo 已经很好地解释了解决方案,但我可能会添加一个澄清:
sometimes node modules are installed in有时节点模块安装在

/usr/local/lib/node_modules

and when you launch node blabla.js modules are searched in当你启动 node blabla.js 模块时,会在

/lib

So a solution is to create a symbolic link:所以一个解决方案是创建一个符号链接:

sudo ln -s /usr/local/lib/node_modules/ /lib/node_modules

In your case your express module is installed at C:\\Users\\Dmitry\\AppData\\Roaming\\npm\\node_modules\\express , but you need to get this module in to your project directory.在您的情况下,您的express模块安装在C:\\Users\\Dmitry\\AppData\\Roaming\\npm\\node_modules\\express ,但您需要将此模块放入您的项目目录中。 So you should copy the file the express module folders from C:\\Users\\Dmitry\\AppData\\Roaming\\npm\\node_modules\\ to your project directory as : C:\\ChatServer\\Server\\node_modules .因此,您应该将express模块文件夹中的文件从C:\\Users\\Dmitry\\AppData\\Roaming\\npm\\node_modules\\到您的项目目录中: C:\\ChatServer\\Server\\node_modules If you do not have a folder named 'node_modules' in your project folder, then create it first and paste those files into this folder.如果您的项目文件夹中没有名为“node_modules”的文件夹,请先创建它并将这些文件粘贴到此文件夹中。 This method worked for me on my windows pc.这种方法在我的 Windows 电脑上对我有用。 Restart your node server and once again run the command node C:\\ChatServer\\Server>node server.js .重新启动您的节点服务器并再次运行命令 node C:\\ChatServer\\Server>node server.js It should work now !!!!它现在应该可以工作了!!!!

On Ubuntu-based OS you can try在基于 Ubuntu 的操作系统上,您可以尝试

sudo apt-get install node-express

its working for me on Mint它在 Mint 上对我有用

Given you have installed node on your system, install Express locally for your project using the following for Windows:鉴于您已经在系统上安装了节点,请使用以下适用于 Windows 的项目在本地安装 Express:

npm install express

or或者

npm install express --save

You might give it global access by using:您可以使用以下方法为其提供全局访问权限:

npm install -g express --save

I had this error in vscode, although the modules where installed.尽管安装了模块,但我在 vscode 中遇到了这个错误。 I am using typescript and express.我正在使用打字稿和快递。 In the server.ts files all the imports had red squiggly underlines.server.ts文件中,所有导入都带有红色波浪线下划线。 It turns out I had a faulty tsconfig.json file.事实证明我有一个错误的tsconfig.json文件。

{
    "compileOnSave": false,
    "compilerOptions": {
        "module": "commonjs", // Previously this value was `es6`
        "target": "es6",
        "allowSyntheticDefaultImports": true,
        "baseUrl": "public",
        "sourceMap": true,
        "outDir": "dist",
        "jsx": "react",
        "strict": true,
        "preserveConstEnums": true,
        "removeComments": true,
        "noImplicitAny": true,
        "allowJs": true
    },
    "exclude": [
        "node_modules",
        "build"
    ]
}

installing express globally will not work on your local project so you need to install it locally for use .全局安装 express 不适用于您的本地项目,因此您需要在本地安装它才能使用。

npm install express

Hope this will work希望这会奏效

Thank you谢谢

create one folder in your harddisk eg sample1 and go to command prompt type :cd and gives the path of sample1 folder and then install all modules...在您的硬盘中创建一个文件夹,例如 sample1 并转到命令提示符键入 :cd 并给出 sample1 文件夹的路径,然后安装所有模块...

npm install express npm 安装快递

npm install jade npm 安装玉

npm install socket.io npm 安装 socket.io

and then whatever you are creating application save in sample1 folder然后无论您创建什么应用程序都保存在 sample1 文件夹中

try it...尝试一下...

转到您的应用程序目录并使用以下命令安装 express 模块npm install express --save然后使用以下命令列出所有安装模块npm ls您将看到所有本地安装模块。

I had the same error following the example on this book: "Kubernetes Up & Running".在这本书的示例之后,我遇到了同样的错误:“Kubernetes Up & Running”。
I see many answers suggesting to install express "by hand" but I'm not convinced is the best solution.我看到很多答案都建议“手动”安装express ,但我不相信这是最好的解决方案。
Because we are using package.json (I can see it in the logs) and the right way to build the app is running npm install , I added the express dependency in the package.json file.因为我们使用的是package.json (我可以在日志中看到它)并且构建应用程序的正确方法是运行npm install ,所以我在package.json文件中添加了express依赖项。

 "dependencies": {
    "express": "^4.17.1"
}

I get the current version with npm search express .我使用npm search express获取当前版本。

I hit the same problem.我遇到了同样的问题。 I had express installed globally at /usr/local/bin/.我已经在 /usr/local/bin/ 全局安装了 express。 When I do 'npm install', express was not created in node_modules of local directory.当我执行“npm install”时,未在本地目录的 node_modules 中创建 express。

  1. Check if you have file name .npmrc in your $HOME检查您的 $HOME 中是否有文件名 .npmrc
  2. If it has 'global = true', change to 'global = false'如果它有“global = true”,则更改为“global = false”
  3. Now do 'npm install' in application directory.现在在应用程序目录中执行“npm install”。 More likely, you should get all package dependent modules installed in node_modules (local) within application directory.更有可能的是,您应该在应用程序目录中的 node_modules (local) 中安装所有依赖包的模块。
  • sudo brew uninstall node sudo brew 卸载节点
  • brew update酿造更新
  • brew upgrade酿造升级
  • brew cleanup酿造清理
  • brew install node brew安装节点
  • sudo chown -R $(whoami) /usr/local须藤 chown -R $(whoami) /usr/local
  • brew link --overwrite node brew link --overwrite 节点
  • sudo brew postinstall node sudo brew postinstall 节点

This worked for me on MacOS X Sierra这在MacOS X Sierra上对我有用

I'm not proud sharing this, but in my case I had:我并不自豪分享这一点,但就我而言,我有:

 require('express.handlebars')

 //and the correct form is:
 require('express-handlebars'); //Use dash instead.

1.first check if express is install at correct location. 1.首先检查express是否安装在正确的位置。 2. npm install express (run this command). 2. npm install express(运行此命令)。 3. express will save under your "node_modules" folder 3. express 将保存在您的“node_modules”文件夹下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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