部署Azure App Service时无法安装节点模块

时间:2017-12-31 15:16:03

标签: javascript node.js azure npm deployment

我在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会话,并在npm install --production下运行wwwroot,则mongodb已成功安装:(

2 个答案:

答案 0 :(得分:0)

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

我的存储库:

enter image description here

<强>的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"
    }
}

然后,我检查了&#34; DEPLOYMENT&gt;下的部署跟踪。部署选项&#34;通过Azure门户我的Azure应用程序部分并检索正在运行的部署命令活动,如下所示:

enter image description here

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

enter image description here

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

答案 1 :(得分:0)

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