Kelcey Wilson
Kelcey Wilson

Reputation: 69

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:

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! [email protected] build: `react-scripts build`

The difference, is I have no require('./error') or file or folder or dependency installed called error.

I have tried the following:

1.

npm install -g

2.

Delete node_modules

npm cache clean --force

npm install

3. Added to my 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.

5. Tried this:

heroku config:set 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?

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

Upvotes: 2

Views: 8560

Answers (4)

Max Phillips
Max Phillips

Reputation: 7489

For a worker process, using ts-node instead of webpack helped significantly. Everything else turns into a module and config nightmare.

Just make sure ts-node is installed as a dependency and not a devDependency.

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

Upvotes: 0

Georgi Marinov
Georgi Marinov

Reputation: 91

Here:

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. 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.

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. A common error would look something like this:

internal/modules/cjs/loader.js:960 throw err; ^

Error: Cannot find module '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. 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.

The other solution is to turn off pruning of devDependencies altogether. To do this, set the following for npm:

heroku config:set NPM_CONFIG_PRODUCTION=false

...

Good luck!

Upvotes: 0

Kelcey Wilson
Kelcey Wilson

Reputation: 69

Well, it turns out the answer can be found in Heroku's Troubleshooting Node.js Deploys page. It turned out that I had at some point accidentally started tracking node_modules. I can't remember how it happened, but think it happened at one point when I was fooling with my .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:

    git ls-files | grep node_modules
    
  2. Ensure you're not tracking it anymore

    echo "node_modules" >.gitignore
    
  3. Remove the cached node_modules

    git rm -r --cached node_modules
    
  4. Make a new commit

    git commit -am 'untracked node_modules'
    

This solved my problem.

Upvotes: 3

eebbesen
eebbesen

Reputation: 5148

I solved this issue by updating package.json to specify a newer version of node.

 {
   "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.

~/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 [email protected]
npm info using [email protected]

Upvotes: 0

Related Questions