node.js express 中没有定义 express
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21900024/
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
express is not defined in express
提问by a8hok
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
I am getting the following error.
我收到以下错误。
> D:\nodejs\mynode\index.js:2
> var app=express();
^
ReferenceError: express is not defined
at Object.<anonymous> (D:\nodejs\mynode\index.js:2:9)
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)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
回答by Silom
For using express you need to follow these steps:
要使用 express,您需要按照以下步骤操作:
1) Basic setup with express
1) 使用 express 进行基本设置
Maybe you have to use sudo
也许你必须使用 sudo
npm install -g express
npm install -g express
This command will install express globally. Also you can now use express on the command line.
此命令将全局安装 express。您现在也可以在命令行上使用 express 。
You can now use express to setup a basic environment using this command.
您现在可以使用 express 使用此命令设置基本环境。
express [options] [dir]
express [options] [dir]
Options:
选项:
-h, --help output usage information
-V, --version output the version number
-s, --sessions add session support
-e, --ejs add ejs engine support (defaults to jade)
-J, --jshtml add jshtml engine support (defaults to jade)
-H, --hogan add hogan.js engine support
-c, --css <engine> add stylesheet <engine> support (less|stylus) (defaults to plain css)
-f, --force force on non-empty directory
2) Basic setup with the package.json
2) 使用 package.json 进行基本设置
Create two files:
创建两个文件:
package.json
package.json
index.js
index.js
The package.jsonincludes lots of project informations.
其中package.json包含大量项目信息。
This is a example package.json:
这是一个示例 package.json:
{
"name": "MyProject",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "~3.4.4"
}
}
If you join now your project folder and run
npm install
如果您现在加入您的项目文件夹并运行
npm install
npm will look up the "dependencies"and install them.
npm 将查找"dependencies"并安装它们。
Now open you index.js and write following.
现在打开 index.js 并编写以下内容。
var express = require('express'); // Get the module
var app = express(); // Create express by calling the prototype in var express
回答by jingyinggong
should enter your code directory and use "npm install" in your shell!
应该进入你的代码目录并在你的 shell 中使用“npm install”!
回答by Murugan Pandian
use this command in your terminal npm install express
在您的终端 npm install express 中使用此命令
make sure install inside your project folder
确保安装在您的项目文件夹中
回答by Vickrant
check the file permissions also please. Also may be your user login do not have the permission to make changes in the system. You may have to use sudo before the command to do so if you are on ubuntu or not a root user.
还请检查文件权限。也可能是您的用户登录没有权限在系统中进行更改。如果您使用的是 ubuntu 或不是 root 用户,则可能必须在命令之前使用 sudo 才能执行此操作。
回答by Ankita ugale
Just close your terminal and start it by right-clicking on it and selecting "Run as administrator" and then follow to the directory in which you want to create your server and run npm install express done!
只需关闭您的终端并通过右键单击它并选择“以管理员身份运行”来启动它,然后转到您要创建服务器的目录并运行 npm install express done!

