简体   繁体   English

从Azure网站上的文件系统安装Node.js模块

[英]Installing Node.js modules from file system on Azure Websites

I have a Node.js app running as an Azure Website and doing my deployments using Git. 我有一个作为Azure网站运行并使用Gi​​t进行部署的Node.js应用。 The app requires a module that's private (ie written by me, no relevance to anyone else and thus not available in NPM) and common to my other projects. 该应用程序需要一个私有的模块(即,由我编写,与其他任何人都不相关,因此在NPM中不可用),并且与我的其他项目共有。 Let's say the module is located in ./lib/mymodule and it has its own dependencies in its package.json file. 假设该模块位于./lib/mymodule并且在package.json文件中具有自己的依赖项。 The problem is I cannot figure out how to have Azure install the dependencies of my own module. 问题是我无法弄清楚如何让Azure安装我自己的模块的依赖项。 I would like to avoid having to add my node_modules directory under version control (currently .gitignor 'd) or having to add the module's dependencies as my app's dependencies (which is ugly and a bit inconvenient). 我想避免不得不在版本控制下添加我的node_modules目录(当前为.gitignor ),或者不必将模块的依赖项添加为我的应用程序的依赖项(这很丑陋,并且有点不方便)。

It's trivial to get this working on my local dev environment. 在我的本地开发环境中进行此工作很简单。 The first thing I did was simply include my own module as a local dependency in my package.json as described in https://docs.npmjs.com/files/package.json (see section "Local Paths"). 我所做的第一件事就是按照https://docs.npmjs.com/files/package.json中的说明 ,将我自己的模块作为本地依赖项包含在package.json中(请参见“本地路径”一节)。 A simple npm install would now install my own module nice and easy. 一个简单的npm install现在可以轻松便捷地安装我自己的模块。 Since I know that Azure runs npm install every time I deploy a new version, I figured this is all I need. 因为我知道Azure每次部署新版本时都会运行npm install ,所以我认为这就是我所需要的。 Turns out I was wrong: local paths in package.json dependencies section were only introduced in NPM 2.0.0 and Azure is running NPM 1.4.x, so the deployment obviously failed. 原来我错了: package.json dependencies部分中的本地路径仅在NPM 2.0.0中引入,而Azure正在运行NPM 1.4.x,因此部署显然失败了。 Ok then, can I run a different version of NPM in Azure? 好的,那么我可以在Azure中运行其他版本的NPM吗? In theory, yes, but practice seems to be something else. 从理论上讲,是的,但是实践似乎是另外一回事。 I tried out https://github.com/glennblock/azure-node-runtime-selector but to no avail. 我尝试了https://github.com/glennblock/azure-node-runtime-selector,但无济于事。 Here's some of the deployment output: 以下是一些部署输出:

remote: Handling node.js deployment.
remote: KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
remote: ............
remote: node version: 0.11.14
remote: ..............
remote: 2.1.18
remote: npm version: 1.4.6
remote: An error has occured during web site deployment.
remote: npm failed
remote:
remote: Error - Changes committed to remote repository but deployment to website failed.

Apparently it fails to install any NPM version >= 2.0.0. 显然,它无法安装任何NPM版本> = 2.0.0。

Ok then, this is not so bad, I figured. 好的,那还不错,我想。 I can work with an older version of NPM, I just need to run another npm install to install my own module, as NPM documentation hints. 我可以使用旧版本的NPM,我只需要运行另一个npm install来安装我自己的模块,如NPM文档所提示。 So I took the local dependency away from my package.json and simply run npm install ./lib/mymodule . 因此,我从package.json npm install ./lib/mymodule了本地依赖项,只需运行npm install ./lib/mymodule Nice and easy... on my local. 很好,很容易...在我的本地。 I dug up instructions how to run custom commands on deployment in Azure: just run azure site deploymentscript --node and edit deploy.sh it creates. 我挖掘了有关如何在Azure中的部署上运行自定义命令的说明:只需运行azure site deploymentscript --node并编辑它创建的deploy.sh Just to ensure the custom deployment script works at all, I made one deployment without editing anything. 只是为了确保自定义部署脚本可以正常工作,我进行了一个部署,而没有进行任何编辑。 Worked ok. 好的 Then I added a line in deploy.sh (around line 111): 然后我在deploy.sh添加了一行(在第111行附近):

# 3. Install npm packages
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
  cd "$DEPLOYMENT_TARGET"
  eval $NPM_CMD install --production
  eval $NPM_CMD install ./lib/mymodule #This line added by me
  exitWithMessageOnError "npm failed"
  cd - > /dev/null
fi

Nope. 不。 This is what I get: 这是我得到的:

remote: Installing npm packages...
remote: npm ERR! addLocal Could not install ./lib/mymodule
remote: An error has occurred during web site deployment.
remote: npm ERR! Error: ENOENT, stat 'd:\home\site\wwwroot\lib\mymodule'
remote: npm failed
...snip...
remote:
remote: npm ERR! System Windows_NT 6.2.9200
remote: npm ERR! command "d:\\Program Files (x86)\\nodejs\\0.10.28\\node.exe" "D:\\Program Files(x86)\\npm\\1.4.9\\node_modules\\npm\\bin\\npm-cli.js" "install" "./lib/mymodule"
remote: npm ERR! cwd d:\home\site\wwwroot
remote: npm ERR! node -v v0.10.28
remote: npm ERR! npm -v 1.4.9
remote: npm ERR! path d:\home\site\wwwroot\lib\mymodule
remote: npm ERR! code ENOENT
remote: npm ERR! errno 34
remote: npm
remote:
remote: Error - Changes committed to remote repository but deployment to website failed.

This is where I got stuck. 这就是我卡住的地方。 I tried playing around with the path, guessing that Windows is mangling it somehow, but haven't found a working solution yet. 我尝试绕过这条路,猜测Windows正在以某种方式对其进行处理,但尚未找到可行的解决方案。 The weird thing is that ./lib/mymodule would seem to resolve to d:\\home\\site\\wwwroot\\lib\\mymodule which look just fine to me and DOES exist, at least after deployment when I FTP'd in to take a look. 奇怪的是, ./lib/mymodule似乎下定决心, d:\\home\\site\\wwwroot\\lib\\mymodule这蛮好看的我,确实存在,至少在部署后,当我在FTP'd拿看。

I would appreciate any and all help in either getting NPM 2.xx working or my making custom deployment go through. 在NPM 2.xx正常工作或进行自定义部署方面,我将不胜感激。 Or maybe there's an alternative to managing my local modules I haven't thought of? 也许还有其他方法可以管理我从未想到的本地模块? And if all else fails, maybe I'll just commit my node_modules or manage my dependencies manually. 如果所有其他方法都失败了,也许我只是提交我的node_modules或手动管理我的依赖项。 I've already used way too much time to resolve what first seemed like a trivial issue. 我已经花了太多时间来解决最初看起来很琐碎的问题。 :) :)

Azure WebSites team will be shipping support for npm 2.1.17 out-of-the box by end of the month (Jan'15). Azure WebSites团队将在本月底(15年1月)开箱即用地提供对npm 2.1.17的支持。 Hope this will help you unblock you. 希望这可以帮助您解除封锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM