【发布时间】: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