部署Azure App Service时无法安装节点模块 [英] Cannot install node modules when deploying Azure App Service
问题描述
我在Azure中有一个Node.js 应用服务,我是在
package.json:
{
name:app-service-hello-world,
description:简单的Hello World节点。用于Azure应用服务的js示例,
版本:0.0.1,
私有:true,
许可证:MIT,
作者:Bruce Chen,
引擎:{
节点:> = 6.9.1
},
脚本:{
start:node index.js
},
依赖项:{
mongodb:^ 3.0.1
}
}
然后,我检查了部署>部署下的部署跟踪选项通过Azure门户我的Azure应用程序部分,并检索正在运行的部署命令活动,如下所示:
使用
此外,我的部署脚本可以自动成功生成。我不需要自定义部署脚本。
I have a Node.js App Service in Azure which I created following this tutorial and then adding some more stuff. My app consists in one file:
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);
Keep in mind the highlighted line for later.
Deployment
Since I am using MongoDB, I have this line in my package.config
:
"dependencies": {
"mongodb": "^3.0.1"
}
Azure must run npm install
when deploying. So, following the Azure Guidelines for deployment, I have added a .deployment
file:
[config]
command = bash deploy.sh
Note that my app is hosted on Linux. File deploy.sh
takes care of running the command for installing 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
The problem
If I keep that line commented, everything is fine. But if I uncomment it, then deployment fails. Locally it works on my box!, but not in the cloud. When inspecting deployment logs, I can see that the npm install
command is executed:
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
But mongodb
does not get installed. Why?.
Using KUDU
If I log in the Kudu tools for my app service and open a Bash session, and run npm install --production
under wwwroot
, then mongodb
is successfully installed :(
According to your description, I followed this tutorial to create my Node.js web app in Azure App Service on Linux. Azure App Service understands package.json and npm-shrinkwrap.json files and can install modules based on entries in these files. I just created the following package.json
file and committed it to my bitbucket repository.
My repository:
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"
}
}
Then, I checked the deployment traces under the "DEPLOYMENT > Deployment options" section of my azure web app via Azure Portal and retrieved the Running deployment command activity as follows:
Using KUDU, I found the dependencies have been successfully installed as follows:
Moreover, my deployment script could automatically generate successfully. I do not need to Custom Deployment Script.
这篇关于部署Azure App Service时无法安装节点模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!