在 Azure 应用程序中指定 Node.js 版本Specifying a Node.js version in an Azure application

托管 Node.js 应用程序时,你可能希望确保应用程序使用特定版本的 Node.js。When hosting a Node.js application, you may want to ensure that your application uses a specific version of Node.js. 有几种方法可为托管在 Azure 上的应用程序完成此操作。There are several ways to accomplish this for applications hosted on Azure.

默认版本Default versions

由 Azure 提供的 Node.js 版本会不断更新。The Node.js versions provided by Azure are constantly updated. 除非另行指定,否则使用 WEBSITE_NODE_DEFAULT_VERSION 环境变量中指定的默认版本。Unless otherwise specified, the default version that is specified in the WEBSITE_NODE_DEFAULT_VERSION environment variable will be used. 若要覆盖此默认值,请遵循本文后续部分中的步骤To override this default value, follow the steps in following sections of this article

Note

如果要在 Azure 云服务(Web 角色或辅助角色)中托管应用程序,并且这是你首次部署应用程序,Azure 将尝试使用你在部署环境中所安装的 Node.js 的版本(如果该版本与 Azure 中提供的默认版本之一相匹配)。If you are hosting your application in an Azure Cloud Service (web or worker role,) and it is the first time you have deployed the application, Azure will attempt to use the same version of Node.js as you have installed on your development environment if it matches one of the default versions available on Azure.

使用 package.json 进行版本控制Versioning with package.json

可通过将以下内容添加到 package.json 文件中来指定要使用的 Node.js 的版本:You can specify the version of Node.js to be used by adding the following to your package.json file:

"engines":{"node":version}

其中, version 是要使用的特定版本号。Where version is the specific version number to use. 可为版本指定更复杂的条件,例如:You can specify more complex conditions for version, such as:

"engines":{"node": "0.6.22 || 0.8.x"}

由于 0.6.22 不是托管环境中提供的版本之一,改为使用 0.8 系列中的最高版本 - 0.8.4。Since 0.6.22 is not one of the versions available in the hosting environment, the highest version of the 0.8 series that is available will be used instead - 0.8.4.

使用应用设置对网站进行版本控制Versioning Websites with App Settings

如果是在网站中托管应用程序,可将环境变量 WEBSITE_NODE_DEFAULT_VERSION 设置为所需的版本。If you are hosting the application in a Website, you can set the environment variable WEBSITE_NODE_DEFAULT_VERSION to the desired version.

使用 PowerShell 对云服务进行版本控制Versioning Cloud Services with PowerShell

如果在云服务中托管应用程序,并且使用 Microsoft Azure PowerShell 部署该应用程序,则可使用 Set-AzureServiceProjectRole PowerShell cmdlet 替代默认的 Node.js 版本。If you are hosting the application in a Cloud Service, and are deploying the application using Azure PowerShell, you can override the default Node.js version by using the Set-AzureServiceProjectRole PowerShell cmdlet. 例如:For example:

Set-AzureServiceProjectRole WebRole1 Node 0.8.4

请注意,上述语句中的参数区分大小写。Note the parameters in the above statement are case-sensitive. 通过检查角色的 package.json 中的 engines 属性,可验证是否已选择正确的 Node.js 版本。You can verify the correct version of Node.js has been selected by checking the engines property in your role's package.json.

还可使用 Get-AzureServiceProjectRoleRuntime 检索托管为云服务的应用程序可用的 Node.js 版本的列表。You can also use the Get-AzureServiceProjectRoleRuntime to retrieve a list of Node.js versions available for applications hosted as a Cloud Service. 始终确认项目所依赖的 Node.js 版本是否在此列表中。Always verify the version of Node.js your project depends on is in this list.

对 Azure 网站使用自定义版本Using a custom version with Azure Websites

虽然 Azure 提供了 Node.js 的几个默认版本,但你可能希望使用并非默认提供的版本。While Azure provides several default versions of Node.js, you may want to use a version that is not provided by default. 如果应用程序托管为 Azure 网站,则可以使用 iisnode.yml 文件完成此操作。If your application is hosted as an Azure Website, you can accomplish this by using the iisnode.yml file. 以下步骤演练了对 Azure 网站使用自定义版本的 Node.Js 的过程:The following steps walk through the process of using a custom version of Node.Js with an Azure Website:

  1. 创建一个新目录,并在该目录中创建 server.js 文件。Create a new directory, and then create a server.js file within the directory. server.js 文件应包含以下内容:The server.js file should contain the following:

     var http = require('http');
     http.createServer(function(req,res) {
       res.writeHead(200, {'Content-Type': 'text/html'});
       res.end('Hello from Azure running node version: ' + process.version + '</br>');
     }).listen(process.env.PORT || 3000);
    

    这会显示在您浏览网站时使用的 Node.js 版本。This will display the Node.js version being used when you browse the website.

  2. 创建一个新网站并记录该网站的名称。Create a new Website and note the name of the site. 例如,以下命令使用 Azure 命令行工具 创建一个名为 mywebsite的新 Azure 网站,并为该网站启用 Git 存储库。For example, the following uses the [Azure Command-line tools] to create a new Azure Website named mywebsite, and then enable a Git repository for the website.

     azure site create mywebsite --git
    
  3. 创建一个名为 bin 的新目录,将其作为包含 server.js 文件的目录的子目录。Create a new directory named bin as a child of the directory containing the server.js file.

  4. 下载要用于应用程序的特定版本 node.exe(Windows 版本)。Download the specific version of node.exe (the Windows version) that you wish to use with your application. 例如,以下命令使用 curl 下载版本 0.8.1:For example, the following uses curl to download version 0.8.1:

     curl -O http://nodejs.org/dist/v0.8.1/node.exe
    

    node.exe 文件保存到先前创建的 bin 文件夹中。Save the node.exe file into the bin folder created previously.

  5. server.js 文件所在的同一目录中创建 iisnode.yml 文件,然后将以下内容添加到 iisnode.yml 文件中:Create an iisnode.yml file in the same directory as the server.js file, and then add the following content to the iisnode.yml file:

     nodeProcessCommandLine: "D:\home\site\wwwroot\bin\node.exe"
    

    将应用程序发布到 Azure 网站后,项目中的 node.exe 文件将位于此路径。This path is where the node.exe file within your project will be located once you have published your application to the Azure Website.

  6. 发布应用程序。Publish your application. 例如,由于我在前面创建了一个带 --git 参数的新网站,因此以下命令会将应用程序文件添加到我的本地 Git 存储库,并将它们推送到网站存储库:For example, since I created a new website with the --git parameter earlier, the following commands will add the application files to my local Git repository, and then push them to the website repository:

     git add .
     git commit -m "testing node v0.8.1"
     git push azure master
    

    发布应用程序后,在浏览器中打开该网站。After the application has published, open the website in a browser. 应看到一则指示“Hello from Azure running node version: v0.8.1”的消息。You should see a message stating "Hello from Azure running node version: v0.8.1".

后续步骤Next Steps

了解如何指定应用程序使用的 Node.js 版本后,请了解如何使用模块构建并部署 Node.js 网站以及如何使用针对 Mac 和 Linux 的 Azure 命令行工具Now that you understand how to specify the version of Node.js used by your application, learn how to [work with modules], build and deploy a Node.js Web Site, and [How to use the Azure Command-Line Tools for Mac and Linux].

有关详细信息,请参阅 Node.js 开发人员中心For more information, see the Node.js Developer Center.