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)
For heroku/dokku you might use
prestartnpm script to build next.js app.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:
https://github.com/mars/heroku-nextjs seems to work
I think we should consider to make
reactandbabel-runtimepeerDependencies, 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.jsonUpgrade to
next@canaryfor 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
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.
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 appnameand evenheroku repo:reset -a appnamebut they didn’t have any effect.Using
heroku-postbuilddoesn’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
reactintonext/react. I wonder how that would work in running local tests though (how do you resolvenextto itself if it doesn’t exist innode_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:I also added in my
package.json: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
.nextbundle and then delays the start command../prestart.js
./package.json
Assumes you’re using a custom server.js file. If not, just run
next startas yourstartscript.Tried this today. As for @KidkArolis, my solution was adding a
Procfilewith:as well as
heroku-postbuildtopackage.json:As you can see, no further changes to
dependencieswere 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.
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