在不同环境下使用编译的节点依赖项
我有一个在本地运行并部署到 CentOS 服务器的节点项目。我有一些依赖项,其中之一依赖于已编译的依赖项。
使用 npm,我可以使用 npm install 在本地编译依赖项,而且效果很好。然而,这不是为部署服务器编译的,因此如果上传它就会中断。
我可以在 CentOS 机器上编译依赖项,它可以在服务器上运行,但在本地环境中会崩溃。
谁能想到一种解决方法,以便我可以强制节点使用本地环境中的全局依赖项以及服务器上的本地依赖项?
谢谢!
更新:
我现在已经找到了一种方法来做到这一点,它并不优雅,但它有效:
- 在本地环境上运行 npm install
- 将 node_modules 文件夹重命名为 node_modules_local
- 在服务器上运行 npm install
- 在所有
前面添加一个变量require()
paths - 将本地环境变量设置为“development”,
- 检查我们是否处于开发模式,如果是,则将 require 路径变量设置为
./node_modules_local
。
I have a node project that I run locally and deploy to a CentOS server. I have a few dependencies, one of which relies on a compiled dependency.
Using npm, I can compile the dependency locally using npm install, and it'll work great. This however is not compiled for the deployment server, so it will break if it is uploaded.
I can compile the dependency on a CentOS box, and it'll work on the server, but it'll break in the local environment.
Can anyone think of a workaround so that I can force node to use the global dependencies in the local environment, and a the local ones on the server?
Thanks!
Update:
I've figured out a way to do this for now, it's not elegant, but it works:
- run npm install on my local environment
- rename the node_modules folder to node_modules_local
- run npm install on the server
- prepend a variable to all
require()
paths - set a local environment variable to "development"
- check if we're in development mode, if we are, set the require path variable to
./node_modules_local
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查依赖项的源代码(编译依赖项和仅 js 依赖项)。当您部署应用程序时,在服务器上运行“npm重建”。似乎这是官方推荐的方法。请参阅 http://www.mikealrogers.com/posts/nodemodules-in-git。 html 获取详细信息
Check in the source code of the dependencies (compiled dependencies and js-only-dependencies). When you deploy your app run 'npm rebuild' on the server. Seems like this is the officially recommended way to go. See http://www.mikealrogers.com/posts/nodemodules-in-git.html for details