安装

¥Installing

假设你已经安装了 Node.js,创建一个目录来保存你的应用,并将其设为你的工作目录。

¥Assuming you’ve already installed Node.js, create a directory to hold your application, and make that your working directory.

$ mkdir myapp
$ cd myapp

使用 npm init 命令为你的应用创建一个 package.json 文件。有关 package.json 如何工作的更多信息,请参阅 npm 的 package.json 处理细节

¥Use the npm init command to create a package.json file for your application. For more information on how package.json works, see Specifics of npm’s package.json handling.

$ npm init

此命令会提示你输入许多信息,例如应用的名称和版本。现在,你只需点击 RETURN 即可接受其中大多数的默认值,但以下情况除外:

¥This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:

entry point: (index.js)

输入 app.js,或任何你想要的主文件名称。如果你希望它是 index.js,请按 RETURN 以接受建议的默认文件名。

¥Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name.

现在,在 myapp 目录中安装 Express 并将其保存在依赖列表中。例如:

¥Now, install Express in the myapp directory and save it in the dependencies list. For example:

$ npm install express

临时安装 Express 并且不将其添加到依赖列表中:

¥To install Express temporarily and not add it to the dependencies list:

$ npm install express --no-save

在 npm 5.0+ 版本中,npm install 默认将模块添加到 package.json 文件中的 dependencies 列表中;对于早期版本的 npm,你必须显式指定 --save 选项。然后,在 app 目录中运行 npm install 将自动安装依赖列表中的模块。

¥By default with version npm 5.0+, npm install adds the module to the dependencies list in the package.json file; with earlier versions of npm, you must specify the --save option explicitly. Then, afterwards, running npm install in the app directory will automatically install modules in the dependencies list.