git - 在 Heroku 上创建 Node.js 应用程序时,我应该将文件夹 "node_modules" checkin Git 吗?

标签 git node.js heroku npm gitignore

我在这里遵循了关于 Heroku 上 Node.js 的基本入门说明:

https://devcenter.heroku.com/categories/nodejs

这些指令没有告诉你创建一个 .gitignore node_modules,因此暗示文件夹 node_modules 应该被 checkin 到 Git。当我在 Git 存储库中包含 node_modules 时,我的入门应用程序运行正常。

当我遵循更高级的示例时:

它指示我将文件夹 node_modules 添加到文件 .gitignore。所以我从 Git 中删除了文件夹 node_modules,将其添加到文件 .gitignore 中,然后重新部署。这次部署失败如下:

-----> Heroku receiving push
-----> Node.js app detected
-----> Resolving engine versions
       Using Node.js version: 0.8.2
       Using npm version: 1.0.106
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
       Error: npm doesn't work with node v0.8.2
       Required: node@0.4 || 0.5 || 0.6
           at /tmp/node-npm-5iGk/bin/npm-cli.js:57:23
           at Object.<anonymous> (/tmp/node-npm-5iGk/bin/npm-cli.js:77:3)
           at Module._compile (module.js:449:26)
           at Object.Module._extensions..js (module.js:467:10)
           at Module.load (module.js:356:32)
           at Function.Module._load (module.js:312:12)
           at Module.require (module.js:362:17)
           at require (module.js:378:17)
           at Object.<anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)
           at Module._compile (module.js:449:26)
       Error: npm doesn't work with node v0.8.2
       Required: node@0.4 || 0.5 || 0.6
           at /tmp/node-npm-5iGk/bin/npm-cli.js:57:23
           at Object.<anonymous> (/tmp/node-npm-5iGk/bin/npm-cli.js:77:3)
           at Module._compile (module.js:449:26)
           at Object.Module._extensions..js (module.js:467:10)
           at Module.load (module.js:356:32)
           at Function.Module._load (module.js:312:12)
           at Module.require (module.js:362:17)
           at require (module.js:378:17)
           at Object.<anonymous> (/tmp/node-npm-5iGk/cli.js:2:1)
           at Module._compile (module.js:449:26)
       Dependencies installed
-----> Discovering process types
       Procfile declares types -> mongod, redis, web
-----> Compiled slug size is 5.0MB
-----> Launching... done, v9

运行“heroku ps”确认崩溃。好的,没问题,所以我回滚了更改,将文件夹 node_module 添加回 Git 存储库并将其从文件 .gitignore 中删除。但是,即使在还原后,我仍然在部署时收到相同的错误消息,但现在应用程序再次正常运行。运行“heroku ps”告诉我应用程序正在运行。

这样做的正确方法是什么?是否包含文件夹 node_modules?为什么回滚时仍然会收到错误消息?我的猜测是 Git 存储库在 Heroku 方面的状态很差。

最佳答案

第二次更新

常见问题解答不再可用。

来自 shrinkwrap 的文档:

If you wish to lock down the specific bytes included in a package, for example to have 100% confidence in being able to reproduce a deployment or build, then you ought to check your dependencies into source control, or pursue some other mechanism that can verify contents rather than versions.

Shannon 和 Steven 之前提到过这一点,但我认为它应该是公认答案的一部分。


更新

为以下推荐列出的来源has been updated .他们不再建议提交 node_modules 文件夹。

Usually, no. Allow npm to resolve dependencies for your packages.

For packages you deploy, such as websites and apps, you should use npm shrinkwrap to lock down your full dependency tree:

https://docs.npmjs.com/cli/shrinkwrap


原帖

作为引用,npm FAQ 清楚地回答了你的问题:

Check node_modules into git for things you deploy, such as websites and apps. Do not check node_modules into git for libraries and modules intended to be reused. Use npm to manage dependencies in your dev environment, but not in your deployment scripts.

关于这方面的一些好的理由,请阅读 Mikeal Rogers' post on this .


来源:https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git

关于git - 在 Heroku 上创建 Node.js 应用程序时,我应该将文件夹 "node_modules" checkin Git 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11459475/

相关文章:

git - 在一个 git 存储库中有多个 'sub-repositories'?

javascript - Node.js 与 mysql 来自 felixge 的设计和奇怪的行为

node.js - 在 Heroku 上部署 ember 应用程序

git - 如何从本地存储库推送到 GitHub 和实时服务器?

git - 如何为 Jenkins (linux) 指定默认用户到 Windows slave

javascript - 有没有办法在 React 组件中运行 AJAX 脚本?

postgresql - 玩框架postgresql : Database 'default' needs evolution

python - 如何自动从 Heroku 上的 git 子模块中提取最新提交?

Git:批量更改提交日期

node.js - 如何在 Node 的 https 模块中记录 SSL 握手