next.js: Error: Cannot find module when deploying to heroku

Using latest next.js relase and a simple hello world index page with 0 dependencies fails to run on heroku with the following error:

{ Error: Cannot find module '/tmp/build_xxx/node_modules/babel-runtime/helpers/inherits'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/app/.next/dist/pages/index.js:7:18)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3) code: 'MODULE_NOT_FOUND' }

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 28
  • Comments: 78 (39 by maintainers)

Most upvoted comments

For heroku/dokku you might use prestart npm script to build next.js app.

"scripts": {
  "prestart": "next build",
  "start": "next start -p ${PORT}",
}

Maybe devDependencies is needed. Heroku by default only installs dependencies when deploying.

Give this a go: https://devcenter.heroku.com/articles/nodejs-support#devdependencies

Source if the issues is Heroku production DOES NOT install devDependecies.

So move to dependencies all build modules you need.

Just ugly but Heroku works that way…

I had problems with Heroku not finding NPM’s after upgrading from Next.js 3 beta to 3.2.2, but disabling Heroku’s caching of Node modules solved it:

heroku config:set NODE_MODULES_CACHE=false

I think we should consider to make react and babel-runtime peerDependencies, which would solve https://github.com/zeit/next.js/issues/204 too.

@arunoda @rauchg thoughts ?

You no longer need the buildpack or a custom procfile (next v2+) for Heroku. www.actionherojs.com is a Next project, and its full source is here https://github.com/actionhero/www.actionherojs.com.

Just include the following in your package.json

 "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "postinstall": "npm run build",
    "start": "NODE_ENV=production node server.js"
  }

Upgrade to next@canary for now, there’ll be a new stable release very soon 👍

Looks like this issue is more closer to https://github.com/zeit/next.js/issues/3533

I’m on next 5, getting the same issue on heroku

A snippet of the error on heroku after deploy

2018-02-15T19:57:06.616452+00:00 app[web.1]:     at require (internal/module.js:20:19)
2018-02-15T19:57:06.616456+00:00 app[web.1]:     at Object.187 (/app/.next/dist/bundles/pages/_document.js:86:18)
2018-02-15T19:57:06.616458+00:00 app[web.1]:     at __webpack_require__ (/app/.next/dist/bundles/pages/_document.js:23:31)
2018-02-15T19:57:06.616460+00:00 app[web.1]:     at Object.186 (/app/.next/dist/bundles/pages/_document.js:78:18)
2018-02-15T19:57:06.616461+00:00 app[web.1]:     at __webpack_require__ (/app/.next/dist/bundles/pages/_document.js:23:31)
2018-02-15T19:57:06.616463+00:00 app[web.1]:     at /app/.next/dist/bundles/pages/_document.js:70:18
2018-02-15T19:57:06.616465+00:00 app[web.1]:     at Object.<anonymous> (/app/.next/dist/bundles/pages/_document.js:73:10) code: 'MODULE_NOT_FOUND' }
2018-02-15T19:57:06.623908+00:00 app[web.1]: { Error: Cannot find module '/tmp/build_9ecf90c5383be7151436c12b01c561e1/node_modules/next/dist/pages/_error.js'
2018-02-15T19:57:06.623910+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:476:15)
2018-02-15T19:57:06.623911+00:00 app[web.1]:     at Function.Module._load (module.js:424:25)
2018-02-15T19:57:06.623912+00:00 app[web.1]:     at Module.require (module.js:504:17)
2018-02-15T19:57:06.623914+00:00 app[web.1]:     at require (internal/module.js:20:19)
2018-02-15T19:57:06.623915+00:00 app[web.1]:     at Object.185 (/app/.next/dist/bundles/pages/_error.js:86:18)
2018-02-15T19:57:06.623916+00:00 app[web.1]:     at __webpack_require__ (/app/.next/dist/bundles/pages/_error.js:23:31)
2018-02-15T19:57:06.623917+00:00 app[web.1]:     at Object.184 (/app/.next/dist/bundles/pages/_error.js:78:18)
2018-02-15T19:57:06.623919+00:00 app[web.1]:     at __webpack_require__ (/app/.next/dist/bundles/pages/_error.js:23:31)
2018-02-15T19:57:06.623920+00:00 app[web.1]:     at /app/.next/dist/bundles/pages/_error.js:70:18
2018-02-15T19:57:06.623921+00:00 app[web.1]:     at Object.<anonymous> (/app/.next/dist/bundles/pages/_error.js:73:10) code: 'MODULE_NOT_FOUND' }

Hi, I deleted a load of files and I’m getting this error on pushing to Heroku. It seems like a previous build is looking for one of those files? I don’t understand why this is happening and where did I go wrong. I have faced this problem one time before, but apparently, it fixed itself then.

Error: ./pages/profile.js?entry
        Module not found: Error: Can't resolve '../components/BeenThereDoneThat' in '/tmp/build_1da9423f972469c21ed0ce9fed0e7555/pages'

It then tries to find the file in multiple directories, using multiple file extensions. It doesn’t exist anymore.

I have tried to use heroku repo:purge_cache -a appname and even heroku repo:reset -a appname but they didn’t have any effect.

Using heroku-postbuild doesn’t work either. What is the reason? Help me out! Thanks! 😃

Yeah, I reckon either this issue (which has more discussion in it) or #360 (which has less discussion but defines the actual problem more in detail) should be re-opened!

And yep, doing that in Procfile isn’t ideal because a) you end up running the build every time you relaunch a dyno, even though it wouldn’t have changed, and b) if you timeout while starting up the dyno (which happened to me!) then your dyno just crashes.

The best workaround so far is to use the heroku-nextjs-build pack.

I like @nkzawa’s suggestion, because all you’d have to do in the Babel loader is transform react into next/react. I wonder how that would work in running local tests though (how do you resolve next to itself if it doesn’t exist in node_modules?) There would need to be some flag somewhere (maybe an env var) that only rewrites when not doing tests… There is quite a lot of complexity going on here. 😃

Hi, just to contribute. I managed to run my app on Heroku with the recent commits on beta which allow to define a .babelrc thank to @benhjames comment earlier on the issue. I added a .babelrcfile at the root containing:

{
  "presets" : [
    "es2015",
    "react",
    "stage-0"
  ],
  "plugins": [
    "react-require", 
    "transform-async-to-generator", 
    "transform-object-rest-spread", 
    "transform-class-properties", 
    "transform-runtime"
  ]
}

I also added in my package.json:

{
  ...
  "scripts": {
    ...
    "heroku-postbuild": "next build"
  },
  "dependencies": {
    "babel": "^6.5.2",
    "babel-loader": "^6.2.10",
    "babel-plugin-react-require": "^3.0.0",
    "babel-plugin-transform-async-to-generator": "^6.16.0",
    "babel-plugin-transform-class-properties": "^6.19.0",
    "babel-plugin-transform-export-extensions": "^6.8.0",
    "babel-plugin-transform-object-rest-spread": "^6.20.2",
    "babel-plugin-transform-runtime": "^6.15.0",
    "babel-polyfill": "^6.20.0",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-0": "^6.16.0",
    "babel-preset-stage-3": "^6.17.0",
    "babel-runtime": "^6.20.0",
    ...
  }
}

If it can be of any help to any anyone.

@eezing unfortunately I don’t have time at the moment to dig into this.

@craigcartmell it worked just fine for me.

I know it doesnt seems to be the “best” solution, but as a quick fix, it fits perfectly.

thank you.

Here’s another hilarious solution if you’re still having trouble. Create a prestart script which builds the .next bundle and then delays the start command.

./prestart.js

// Delay the yarn start script until .next has been fully built
setTimeout(function() {
  let exec = require('child_process').exec, child;

  child = exec('yarn build',
    function (error, stdout, stderr) {
      console.log('stdout: ' + stdout);
      console.log('stderr: ' + stderr);
      if (error !== null) {
        console.log('exec error: ' + error);
      }
    });
}, 5000)

./package.json

Assumes you’re using a custom server.js file. If not, just run next start as your start script.

"scripts": {
    "build": "next build",
    "prestart": "node ./prestart.js",
    "start": "NODE_ENV=production node server.js -p $PORT",
  },

Tried this today. As for @KidkArolis, my solution was adding a Procfile with:

web: npm run build && npm start -- --port $PORT

as well as heroku-postbuild to package.json:

{
	"name": "my-nextjs-app",
	"scripts": {
		"dev": "next",
		"build": "next build",
		"start": "next start",
		"heroku-postbuild": "next build"
	},
	"dependencies": {
		"lodash": "^4.17.4",
		"next": "^3.0.0-beta16",
		"react": "^15.6.1",
		"react-dom": "^15.6.1"
	}
}

As you can see, no further changes to dependencies were needed.

Guys we took https://github.com/zeit/next.js/pull/1164 in. So, we could use Next.js apps on heroku without much trouble.

NODE_PATH is only ever used if require doesn’t resolve to anything normally

I didn’t know this 😐

I just want to make sure the fix is an elegant one, and not a series of hard-to-maintain workarounds. If necessary, we can always patch webpack as well