【问题标题】:Heroku deploy Error: Cannot find module - compilationHeroku 部署错误:找不到模块 - 编译
【发布时间】:2019-01-27 02:56:15
【问题描述】:

这与其他问题类似,但我会解释有什么不同。

像许多其他人一样,我有一个应用程序在我自己的服务器上运行良好,但是当我推送到 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`

不同之处在于我没有安装 require('./error') 或文件或文件夹或依赖项,称为错误。

我尝试了以下方法:

1.

npm 安装 -g

2.

删除节点模块

npm 缓存清理 --force

npm 安装

3。 添加到我的 package.json 中:

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

4。 我从头开始克隆了 3 次正在工作的应用程序,创建了一个新的 Heroku 实例,然后尝试部署。

5。 试过这个:

heroku 配置:设置 NODE_MODULES_CACHE=false

这些解决方案和其他不起作用的解决方案来自这些帖子:

How do I resolve "Cannot find module" error using Node.js?

Heroku Deploy Error: Cannot get Node App running after Deploy : Cannot find module '/app/web.js'

Heroku Deploy Error: Cannot find module './errors/cast'

Error: Cannot find module './shared'

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

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

【问题讨论】:

  • 谢谢 - 我确实有,但它不适用于我的问题。我会把它添加到我的参考文献中。另外,我刚刚在 Heroku 的所有地方的部署故障排除页面中找到了解决方案...

标签: node.js heroku deployment


【解决方案1】:

对于工作进程,使用 ts-node 代替 webpack 有很大帮助。其他一切都变成了模块和配置的噩梦。

只需确保 ts-node 安装为依赖项而不是 devDependency。

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

【讨论】:

    【解决方案2】:

    这里:

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

    这至少暂时挽救了我的生命(“internal/modules/cjs/loader.js:888”问题),直到我弄清楚在我的部门或 Heroku 平台中发生了什么变化。从上个月开始,Heroku 似乎已经停止为我的 Angular 项目正确修剪生产部门,或者可能过早地开始这样做。

    缺少模块

    如果 package.json 中包含的模块从 构建或生产应用程序,它可能已被 Heroku 在 修剪。一个常见的错误如下所示:

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

    错误:找不到模块“express”

    为了为应用创建更小的 slug 大小,buildpack 将修剪 在构建结束时从 package.json 中取出 devDependencies, 这样 slug 将只包含列出的依赖项 运行。如果 devDependencies 中有一个依赖项 修剪发生后需要,将依赖项移动到依赖项, 所以它不会被删除。

    另一种解决方案是关闭 devDependencies 的修剪 共。为此,请为 npm 设置以下内容:

    heroku config:set NPM_CONFIG_PRODUCTION=false
    

    ...

    祝你好运!

    【讨论】:

      【解决方案3】:

      嗯,原来答案可以在 Heroku 的Troubleshooting Node.js Deploys 页面中找到。事实证明,我在某些时候不小心开始跟踪 node_modules。我不记得它是怎么发生的,但我想它发生在我和我的 .gitignore 搞混的时候。该解决方案直接来自故障排除页面:

      1. 检查您是否正在跟踪 node_modules -- 如果这返回结果列表,那么您是:

        git ls-files | grep node_modules
        
      2. 确保您不再跟踪它

        echo "node_modules" >.gitignore
        
      3. 移除缓存的 node_modules

        git rm -r --cached node_modules
        
      4. 进行新的提交

        git commit -am 'untracked node_modules'
        

      这解决了我的问题。

      【讨论】:

      • 谢谢凯尔西——我知道这是一个老话题——但这完全帮助了我!
      【解决方案4】:

      我通过更新 package.json 以指定更新版本的节点解决了这个问题。

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

      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
      

      【讨论】:

        猜你喜欢
        • 2021-03-17
        • 2014-03-22
        • 2016-12-25
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 2018-11-07
        • 2017-06-07
        • 1970-01-01
        相关资源
        最近更新 更多