1

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

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

Kelcey Wilson
  • 59
  • 1
  • 9

2 Answers2

3

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.

Let Me Tink About It
  • 11,866
  • 13
  • 72
  • 169
Kelcey Wilson
  • 59
  • 1
  • 9
0

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 npm@6.9.0
npm info using node@v11.10.0
eebbesen
  • 4,760
  • 7
  • 46
  • 67