我遵循了Heroku上Node.js的基本入门说明:

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

这些指令并没有告诉你创建一个.gitignore node_modules,因此意味着node_modules文件夹应该签入Git。当我在Git存储库中包含node_modules时,我的启动应用程序正常运行。

当我遵循更高级的例子在:

用Node.js, Ruby, MongoDB和Socket构建实时,多语言应用程序。IO https://github.com/mongolab/tractorpush-server(源)

它指示我将文件夹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 ?为什么回滚时仍然会得到错误消息?我猜Heroku这边的Git仓库状态很差。


当前回答

我正在使用这个解决方案:

Create a separate repository that holds folder node_modules. If you have native modules that should be build for specific platform then create a separate repository for each platform. Attach these repositories to your project repository with git submodule: git submodule add .../your_project_node_modules_windows.git node_modules_windows git submodule add .../your_project_node_modules_linux_x86_64 node_modules_linux_x86_64 Create a link from platform-specific node_modules to node_modules directory and add node_modules to .gitignore. Run npm install. Commit submodule repository changes. Commit your project repository changes.

因此,您可以轻松地在不同平台上的node_modules之间切换(例如,如果您在OS X上开发并部署到Linux上)。

其他回答

如果你正在为你的应用程序滚动你自己的模块,你可以:

保留这些(仅保留这些)在应用程序的/node_modules文件夹中,并将所有其他依赖项移出到parent ../ node_modules文件夹。这将工作,因为NodeJS CommonJS模块系统的工作方式是向上移动到父目录,等等,直到到达树的根。参见:https://nodejs.org/api/modules.html 或者gitignore除/node_modules/your-modules之外的所有/node_modules/*。参见:使.gitignore忽略除少数文件外的所有内容

这个用例非常棒。它让你可以很好地保留你专门为应用程序创建的模块,并且不会因为以后可以安装的依赖关系而变得混乱。

在Heroku上创建Node.js应用程序时,我应该在文件夹“node_modules”中检入Git吗?

但是Stack Overflow的格式很奇怪。

如果您没有相同的机器,并且正在检入node_modules,请在本机扩展上执行.gitignore。我们的。gitignore看起来像:

# Ignore native extensions in the node_modules folder (things changed by npm rebuild)
node_modules/**/*.node
node_modules/**/*.o
node_modules/**/*.a
node_modules/**/*.mk
node_modules/**/*.gypi
node_modules/**/*.target
node_modules/**/.deps/
node_modules/**/build/Makefile
node_modules/**/**/build/Makefile

通过首先检入所有内容来测试,然后让另一个开发人员执行以下操作:

rm -rf node_modules
git checkout -- node_modules
npm rebuild
git status

确保没有文件更改。

你不应该在你的.gitignore文件中包含node_modules文件夹(或者你应该在部署到Heroku的源代码中包含node_modules文件夹)。

如果文件夹node_modules:

存在时NPM安装将使用这些提供的库,并将通过NPM rebuild重建任何二进制依赖项。 如果不存在,那么NPM install将不得不自己获取所有依赖项,这将增加slug编译步骤的时间。

具体步骤请参阅Node.js构建包源代码。

然而,最初的错误看起来是npm和Node.js版本之间的不兼容。总是显式地设置包的引擎部分是个好主意。Json文件,以避免这些类型的情况:

{
  "name": "myapp",
  "version": "0.0.1",
  "engines": {
    "node": "0.8.x",
    "npm":  "1.1.x"
  }
}

这将确保开发/生产对等,并减少今后发生此类情况的可能性。

不要签入文件夹node_modules,而是创建一个包。Json文件的应用程序。

包。Json文件指定了应用程序的依赖关系。Heroku可以告诉npm安装所有这些依赖项。你链接到的教程中有一个关于软件包的部分。json文件。

从Git中的"node_modules":

回顾一下。 仅为您部署的应用程序签入node_modules,不可重用 您维护的包。 任何编译的依赖项都应该有它们的依赖项 源签入,而不是编译目标,并且应该$ NPM重新构建 在部署。

我最喜欢的部分:

所有将node_modules添加到gitignore的人,都删除它 妈的,今天,这是一个我们都很乐意离开的时代的产物 在后面。全局模块的时代已经结束了。

(最初的链接是这个,但现在已经死了。谢谢@Flavio指出这一点