简体   繁体   English

Heroku 部署错误:找不到模块 - 编译

[英]Heroku deploy Error: Cannot find module - compilation

This is similar to other questions, but I'll explain what's different.这与其他问题类似,但我将解释不同之处。

Like the many others, I have an app that works fine on my own server, but when I push to Heroku, I get the following error:像许多其他人一样,我有一个在我自己的服务器上运行良好的应用程序,但是当我推送到 Heroku 时,我收到以下错误:

remote:        > react-scripts build
remote:
remote: module.js:549
remote:     throw err;
remote:     ^
remote:
remote: Error: Cannot find module './error'
remote:     at Function.Module._resolveFilename (module.js:547:15)
remote:     at Function.Module._load (module.js:474:25)
remote:     at Module.require (module.js:596:17)
remote:     at require (internal/module.js:11:18)
remote:     at Object.<anonymous> (/tmp/build_96886a1c0acbba91a1be57ec9c1487e8/client/node_modules/browserslist/index.js:7:25)
remote:     at Module._compile (module.js:652:30)
remote:     at Object.Module._extensions..js (module.js:663:10)
remote:     at Module.load (module.js:565:32)
remote:     at tryModuleLoad (module.js:505:12)
remote:     at Function.Module._load (module.js:497:3)
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 1
remote: npm ERR! client@0.1.0 build: `react-scripts build`

The difference, is I have no require('./error') or file or folder or dependency installed called error.不同之处在于我没有require('./error')或文件或文件夹或安装的依赖项,称为错误。

I have tried the following:我尝试了以下方法:

1. 1.

npm install -g npm 安装 -g

2. 2.

Delete node_modules删除 node_modules

npm cache clean --force npm 缓存清理 --force

npm install安装

3. Added to my package.json: 3. 添加到我的 package.json 中:

"engines": {
   "node": "8.11.3"
}

4. Three times from scratch I cloned my working app, created a new Heroku instance, and tried to deploy. 4. 我从头开始克隆了我的工作应用程序 3 次,创建了一个新的 Heroku 实例,并尝试部署。

5. Tried this: 5.试过这个:

heroku config:set NODE_MODULES_CACHE=false heroku 配置:设置 NODE_MODULES_CACHE=false

These solutions and others that didn't work came from these posts:这些解决方案和其他不起作用的解决方案来自这些帖子:

How do I resolve "Cannot find module" error using Node.js? 如何使用 Node.js 解决“找不到模块”错误?

Heroku Deploy Error: Cannot get Node App running after Deploy : Cannot find module '/app/web.js' Heroku 部署错误:部署后无法运行节点应用程序:找不到模块“/app/web.js”

Heroku Deploy Error: Cannot find module './errors/cast' Heroku 部署错误:找不到模块“./errors/cast”

Error: Cannot find module './shared' 错误:找不到模块“./shared”

https://github.com/nodejs/help/issues/1271 https://github.com/nodejs/help/issues/1271

https://help.heroku.com/TO64O3OG/cannot-find-module-in-node-js-at-runtime https://help.heroku.com/TO64O3OG/cannot-find-module-in-node-js-at-runtime

Well, it turns out the answer can be found in Heroku's Troubleshooting Node.js Deploys page.好吧,事实证明答案可以在 Heroku 的Node.js 部署疑难解答页面中找到。 It turned out that I had at some point accidentally started tracking node_modules.结果是我在某个时候不小心开始跟踪 node_modules。 I can't remember how it happened, but think it happened at one point when I was fooling with my .gitignore.我不记得它是怎么发生的,但我认为它发生在我用我的 .gitignore 愚弄的某一时刻。 This solution, straight from the troubleshooting page was:这个解决方案,直接从故障排除页面是:

  1. Check to see if you are tracking node_modules -- if this returns a list of results, you are:检查您是否正在跟踪 node_modules -- 如果返回结果列表,则您是:

     git ls-files | grep node_modules
  2. Ensure you're not tracking it anymore确保您不再跟踪它

    echo "node_modules" >.gitignore
  3. Remove the cached node_modules删除缓存的 node_modules

     git rm -r --cached node_modules
  4. Make a new commit进行新的提交

    git commit -am 'untracked node_modules'

This solved my problem.这解决了我的问题。

I solved this issue by updating package.json to specify a newer version of node.我通过更新 package.json 以指定较新版本的节点解决了这个问题。

 {
   "name": "myapp",
   "engines": {
-    "node": "8.1.4"
+    "node": "11.10.0"
   },

npm install appears to ignore the package.json node version and just use whatever is installed locally so I wasn't having issues locally, but was on the Heroku runtime because the Heroku deployment used the node version specified in package.json. npm install似乎忽略了 package.json 节点版本,只使用本地安装的任何内容,所以我在本地没有问题,但在 Heroku 运行时上,因为 Heroku 部署使用了 package.json 中指定的节点版本。

~/projects/myapp$ node -v
v11.10.0
~/projects/myapp$ npm install -verbose
npm info it worked if it ends with ok
npm verb cli [ '/usr/local/Cellar/node/11.10.0/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   'install',
npm verb cli   '-verbose' ]
npm info using npm@6.9.0
npm info using node@v11.10.0

Here:这里:

https://devcenter.heroku.com/articles/troubleshooting-node-deploys#missing-modules https://devcenter.heroku.com/articles/troubleshooting-node-deploys#missing-modules

This is what saved my life ("internal/modules/cjs/loader.js:888" issue) at least temporarily until I figure out what has changed, in my deps or in the Heroku platform.这至少是暂时挽救了我的生命(“internal/modules/cjs/loader.js:888”问题),直到我弄清楚在我的 deps 或 Heroku 平台中发生了什么变化。 As of within last month Heroku seems to have stopped correctly pruning deps for production for my Angular project, or maybe started doing so a bit prematurely.截至上个月,Heroku 似乎已经停止为我的 Angular 项目正确修剪 deps,或者可能开始这样做有点过早。

Missing Modules缺少模块

If a module that is included in the package.json is missing from the build or the production app, it may have been removed by Heroku during pruning.如果构建或生产应用程序中缺少 package.json 中包含的模块,则它可能已在修剪期间被 Heroku 删除。 A common error would look something like this:一个常见的错误看起来像这样:

internal/modules/cjs/loader.js:960 throw err; internal/modules/cjs/loader.js:960 抛出错误; ^ ^

Error: Cannot find module 'express'错误:找不到模块“express”

In order create a smaller slug size for apps, the buildpack will prune out the devDependencies from the package.json at the end of the build, so that the slug will only include the dependencies that are listed at runtime.为了为应用创建更小的 slug 大小,buildpack 会在构建结束时从 package.json 中删除 devDependencies,这样 slug 将只包含在运行时列出的依赖项。 If there is a dependency that is in the devDependencies that is needed after the prune occurs, move the dependency to dependencies, so it is not removed.如果在剪枝发生后需要的devDependencies中存在依赖,则将该依赖移动到dependencies中,这样就不会被删除。

The other solution is to turn off pruning of devDependencies altogether.另一种解决方案是完全关闭 devDependencies 的修剪。 To do this, set the following for npm:为此,请为 npm 设置以下内容:

heroku config:set NPM_CONFIG_PRODUCTION=false

... ...

Good luck!祝你好运!

For a worker process, using ts-node instead of webpack helped significantly.对于工作进程,使用ts-node而不是 webpack 有很大帮助。 Everything else turns into a module and config nightmare.其他一切都变成了模块和配置噩梦。

Just make sure ts-node is installed as a dependency and not a devDependency.只需确保ts-node安装为依赖项而不是 devDependency。

"scripts": {
    "start": "ts-node index.ts",
  }

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

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