将Express与Node.js结合使用时出现错误 [英] Getting error while using Express with Node.js
问题描述
我用npm express编写了一个小程序.当我运行程序时,出现如下错误.(我是node.js的新手)
I wrote a small program using npm express. while I am running the program i am getting error like below.(I am new to node.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:362:17)
at require (module.js:378:17)
at Object.<anonymous> (C:\Users\node\node_modules\app.js:1:77)
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)
推荐答案
在您的应用程序内部,您显然需要
express
模块,可能是这样的:
Inside your app you obviously require
the express
module, probably like this:
var express = require('express');
要使此行正常工作,您需要将Express安装到应用程序的本地上下文中.为此,请运行
For this line to work you need to install Express into the local context of your application. To do so run
$ npm install express
在您应用程序的文件夹内.这将(如果尚不存在)创建一个文件夹 node_modules
,您的所有依赖项都将存放在该文件夹中.
inside your application's folder. This will (if it does not yet exist) create a folder node_modules
where all your dependencies go.
此外,我建议您将Express放入 dependencies
块内的 package.json
中,例如:
Additionally, I'd suggest that you put Express into your package.json
inside the dependencies
block, such as:
"dependencies": {
"express": "3.1.0"
}
当然,您可以将版本号调整为所使用的任何版本.对所有依赖项完成此操作后,只需运行即可即可立即安装它们
Of course, you can adjust the version number to whatever version you use. Once you've done this for all of your dependencies, you can install them at once by simply running
$ npm install
那应该解决它.
PS:在这种情况下,是否全局安装Express都无关紧要.全局安装仅适用于在整个系统范围内提供 express
引导程序. require
函数始终仅在本地应用程序上下文中搜索.
PS: It does not matter for this scenario whether you installed Express globally or not. A global installation is only good for having the express
bootstrapper available system-wide. The require
function always only searches within the local application context.
这篇关于将Express与Node.js结合使用时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!