node.js,错误:找不到模块“express”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14949118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:11:59  来源:igfitidea点击:

node.js, Error: Cannot find module 'express'

node.jsexpress

提问by qinking126

I am new to Node.js, try to learn express to build my first web application. I got stuck on my very first sample code and need some help to get it running. Before I post this question, I did search on stack overflow, found some similar questions but still could not fix it.

我是 Node.js 的新手,尝试学习 express 来构建我的第一个 Web 应用程序。我被我的第一个示例代码卡住了,需要一些帮助才能让它运行。在我发布这个问题之前,我确实搜索了堆栈溢出,发现了一些类似的问题,但仍然无法解决。

Error: Cannot find module 'express'

错误:找不到模块“express”

I am using mac os 10.8.2. I have Node.js installed using nvm.

我正在使用mac os 10.8.2。我一直在使用Node.js的安装nvm

node.js: 0.8.20 path to node: /Users/feelexit/nvm/v0.8.20/bin/node path to express: /Users/feelexit/nvm/node_modules/express

node.js: 0.8.20 节点路径:/Users/feelexit/nvm/v0.8.20/bin/node 表达路径:/Users/feeexit/nvm/node_modules/express

here's my sample code: this file locates at:

这是我的示例代码:此文件位于:

/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js

/用户/feelexit/WebstormProjects/learnnode/node_modules/index.js

var express = require('express');

var app = express();

app.get('/', function(req, res){

    res.send('welcome to express');
});

app.listen(3000);

when I try to run this command node index.js

当我尝试运行此命令时 node index.js

I get following error message, please help me to fix it.

我收到以下错误消息,请帮助我修复它。

Thank you.

谢谢你。

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:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js:1:81)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
feelexits-Mac:node_modules feelexit$ 

Update to answer chovy's question:

更新回答chovy的问题:

feelexits-Mac:~ feelexit$ npm install
npm ERR! install Couldn't read dependencies
npm ERR! Error: ENOENT, open '/Users/feelexit/package.json'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Darwin 12.2.0
npm ERR! command "/Users/feelexit/nvm/v0.8.20/bin/node" "/Users/feelexit/nvm/v0.8.20/bin/npm" "install"
npm ERR! cwd /Users/feelexit
npm ERR! node -v v0.8.20
npm ERR! npm -v 1.2.11
npm ERR! path /Users/feelexit/package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/feelexit/npm-debug.log
npm ERR! not ok code 0

回答by Saurabh Rana

It says

它说

 Cannot find module 'express'

Do you have express installed?If not then run this.

你有安装快递吗?如果没有,那么运行这个。

 npm install express

And run your program again.

然后再次运行你的程序。

回答by Bill

After you do express in your terminal, then do

在您的终端中执行 express 后,然后执行

 npm install

To install all the dependencies.

安装所有依赖项。

Then you can do node app to run the server.

然后你可以做 node app 来运行服务器。

回答by Reza Ebrahimi

Check if you have installed expressmodule. If not, use this command:

检查您是否安装了express模块。如果没有,请使用以下命令:

npm install express

and if your node_modulesdirectory is in another place, set NODE_PATHenvirnment variable:

如果您的node_modules目录在另一个地方,请设置NODE_PATH环境变量:

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

回答by Bonface Ochieng

 npm install --save express   
这对我有用,只需再次运行 express.js 安装

回答by Eldad

npm install from within your app directory will fix the issue as it will install everything required

npm install 从您的应用程序目录中安装将解决该问题,因为它将安装所需的一切

回答by Wjdavis5

Digging up an old thread here BUT I had this same error and I resolved by navigating to the directory my NodeApp resides in and running npm install -d

在这里挖掘一个旧线程但是我遇到了同样的错误,我通过导航到我的 NodeApp 所在的目录并运行来解决 npm install -d

回答by Shemogumbe

Unless you set Node_PATH, the only other option is to install express in the app directory, like npm install express --save. Express may already be installed but nodecannot find it for some reason

除非您设置Node_PATH,否则唯一的其他选择是在 app 目录中安装 express,例如npm install express --save. Express 可能已经安装但node由于某种原因找不到它

回答by Default

You have your express module located in a different directory than your project. That is probably the problem since you are trying to require()it locally. Try moving your express module from /Users/feelexit/nvm/node_modules/expressto /Users/feelexit/WebstormProjects/learnnode/node_modules/express. This infocan give you more detail about node_module file structures.

您的 express 模块位于与项目不同的目录中。这可能是问题所在,因为您正在require()本地尝试。尝试将您的 express 模块从/Users/feelexit/nvm/node_modules/express 移动/Users/feelexit/WebstormProjects/learnnode/node_modules/express此信息可以为您提供有关 node_module 文件结构的更多详细信息。

回答by VeXii

if youre main file is located at /Users/feelexit/WebstormProjects/learnnode/node_modules/index.jsthen express needs to be located at /Users/feelexit/WebstormProjects/learnnode/node_modules/node_modulesas node always looks for modules in ./node_modules(and its internal folder) when the path dont start with ./or /(more info here)

如果您的主文件位于,/Users/feelexit/WebstormProjects/learnnode/node_modules/index.js那么 express 需要位于,/Users/feelexit/WebstormProjects/learnnode/node_modules/node_modules因为./node_modules当路径不以./or开头时,节点总是在(及其内部文件夹)中查找模块/更多信息在这里

i think you miss placed youre main file in the module folder

我想你错过了把你的主文件放在模块文件夹中

回答by Rijo

for this scenario run npm install express command using your cmd prompt for the respective folder where you want to run the program. Example I want to run the express module program server.js in F:\nodeSample. So run "npm install express" in that particular folder then run server.js

对于这种情况,使用 cmd 提示符运行 npm install express 命令,以找到要运行该程序的相应文件夹。示例 我想在 F:\nodeSample 中运行 express 模块程序 server.js。因此,在该特定文件夹中运行“npm install express”,然后运行 ​​server.js