我在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
[email protected] /home/site/repository
npm ERR! missing: mongodb@^3.0.1, required by [email protected]
`-- UNMET DEPENDENCY mongodb@^3.0.1
但mongodb
没有安装。为什么?。
如果我登录我的应用程序服务的Kudu工具并打开一个Bash会话,并在npm install --production
下运行wwwroot
,那么mongodb
已成功安装:(
根据您的描述,我按照这个tutorial在Linux上的Azure App Service中创建我的Node.js Web应用程序。 Azure App Service了解package.json和npm-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 Portal检查了我的Azure应用程序的“部署>部署选项”部分下的部署跟踪,并检索了运行部署命令活动,如下所示:
使用KUDU,我发现依赖项已成功安装如下:
此外,我的部署脚本可以自动生成。我不需要Custom Deployment Script。