【问题标题】:Cannot install node modules when deploying Azure App Service部署 Azure 应用服务时无法安装节点模块
【发布时间】:2018-06-11 03:12:38
【问题描述】:

我在 Azure 中有一个 Node.js 应用服务,我在 this tutorial 之后创建了它,然后添加了更多内容。我的应用包含在一个文件中:

var http = require("http");
//var mongoClient = require("mongodb").MongoClient; // !!!THIS LINE!!!

var server = http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
});

var port = process.env.PORT || 1337;
server.listen(port);

记住突出显示的行以备后用。

部署

由于我使用的是 MongoDB,我的package.config 中有这一行:

"dependencies": {
  "mongodb": "^3.0.1"
}

Azure 必须在部署时运行 npm install。所以,在Azure Guidelines for deployment 之后,我添加了一个.deployment 文件:

[config]
command = bash deploy.sh

请注意,我的应用托管在 Linux 上。文件deploy.sh 负责运行安装 npm deps 的命令:

if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
  echo Installing NuGetpackages.
  cd "$DEPLOYMENT_TARGET"
  eval $NPM_CMD install --production
  exitWithMessageOnError "npm failed"
  cd - > /dev/null
fi

问题

如果我保留该行的注释,一切都很好。但是如果我取消注释它,那么部署就会失败。 它在本地可以在我的盒子上运行!,但不能在云端运行。检查部署日志时,我可以看到npm install 命令被执行:

Command: bash deploy.sh
Handling node.js deployment.
Handling KuduSync.
Kudu sync from: '/home/site/repository' to: '/home/site/wwwroot'
Ignoring: .deployment
Copying file: '.gitignore'
Copying file: 'LICENSE'
Copying file: 'README.md'
Ignoring: deploy.sh
Copying file: 'package.json'
Copying file: 'process.json'
Deleting file: 'deploy.cmd'
Ignoring: .git
Copying file: 'src/db.js'
Copying file: 'src/index.js'
Copying file: 'src/settings.json'
Copying file: 'src/data/info.js'
Copying file: 'src/data/page.js'
Detecting node version spec...
Using package.json engines.node value: >=6.9.1
Node.js versions available on the platform are: 4.4.7, 4.5.0, 6.2.2, 6.6.0, 6.9.3, 6.10.3, 6.11.0, 8.0.0, 8.1.0.
Resolved to version 8.1.0
Detecting npm version spec...
Using default for node 8.1.0: 5.0.3
NPM versions available on the platform are: 2.15.8, 2.15.9, 3.9.5, 3.10.3, 3.10.10, 5.0.3.
Resolved to version 5.0.3
Installing NuGet packages.
npm WARN lifecycle The node binary used for scripts is /usr/bin/node but npm is using /opt/nodejs/8.1.0/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
up to date in 2.885s
myuser-web-srv@0.1.0 /home/site/repository
npm ERR! missing: mongodb@^3.0.1, required by myuser-web-srv@0.1.0
`-- UNMET DEPENDENCY mongodb@^3.0.1

但是mongodb 没有安装。为什么?

使用 KUDU

如果我为我的应用服务登录 Kudu 工具并打开一个 Bash 会话,并在 wwwroot 下运行 npm install --production,则 mongodb 安装成功:(

【问题讨论】:

    标签: javascript node.js azure npm deployment


    【解决方案1】:

    这是我知道的一个老问题,但我最终来到这里并想分享您可以在应用服务配置中设置一个设置,说 SCM_DO_BUILD_DURING_DEPLOYMENT=true 在某些情况下似乎需要(当 kudu 检测到它的节点时,它不会执行 npm install没有这个设置)。

    【讨论】:

      【解决方案2】:

      根据您的描述,我按照tutorial 在 Linux 上的 Azure 应用服务中创建了我的 Node.js Web 应用。 Azure App Service 理解 package.jsonnpm-shrinkwrap.json 文件,并可以根据这些文件中的条目安装模块。我刚刚创建了以下 package.json 文件并将其提交到我的 bitbucket 存储库。

      我的存储库:

      package.json:

      {
          "name": "app-service-hello-world",
          "description": "Simple Hello World Node.js sample for Azure App Service",
          "version": "0.0.1",
          "private": true,
          "license": "MIT",
          "author": "Bruce Chen",
          "engines": {
              "node": ">=6.9.1"
          },
          "scripts": {
              "start": "node index.js"
          },
          "dependencies": {
              "mongodb": "^3.0.1"
          }
      }
      

      然后,我通过 Azure 门户检查了我的 azure Web 应用程序的“部署 > 部署选项”部分下的部署跟踪,并检索了 运行部署命令 活动,如下所示:

      使用KUDU,发现依赖已经安装成功,如下:

      另外,我的部署脚本可以自动生成成功。我不需要Custom Deployment Script

      【讨论】:

      • 我已经用更多信息更新了我的问题。但就我而言,您的确切程序不起作用:(
      • 您是否尝试过使用KUDU并手动执行/home/site/wwwroot目录下的npm install --production来缩小此问题。
      • 如果我这样做,它会安装 mongodb... 奇怪。为什么?
      • 这些应用服务因 npm 安装失败而臭名昭著。
      • 我面临同样的问题,node_modules 是通过 kudo 安装的,但是一旦我尝试从 azure 门户运行应用程序,node_modules 就会消失!!!!!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 2021-08-23
      • 2015-08-08
      • 2021-11-29
      • 2014-06-08
      • 1970-01-01
      相关资源
      最近更新 更多