next.js: Unable to import module 'now__launcher'
Bug report
Describe the bug
When trying to build and deploy to now, everything seems to build fine but at the end of the build process I get the following error:
Unable to import module 'now__launcher': Error
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/next.config.js:2:51)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Clone this repo
https://github.com/lorenseanstewart/next-blog-starter npm ifrom the rootnow- To see the error, check logs with
now logs <url-provided-by-deploy>
Expected behavior
This project to deploy without errors.
System information
- macOS
- Version of Next.js: 7.0.2
- Version of Now: 2
- Version of now-cli: 12.1.6
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 52 (21 by maintainers)
Commits related to this issue
- Changes to next.config.json zeit/next.js#5750 — committed to iamdevlinph/me by iamdevlinph 8 years ago
- moved to next.js serverless https://github.com/zeit/next.js/issues/5750#issuecomment-451607604 — committed to bevry-archive/jordanbpeterson.community by balupton 8 years ago
Because this is the first result in google, and there is no link to the other issue, I will leave the fix I found here:
The issue comes due to trying to run bundling work during the production serving phase of next (details here), such as
@zeit/next-css,@zeit/next-sass, or@zeit/next-typescript.The fix is to only run those during the build phase of running next:
next.config.jsUpdate: Still not working? See this comment below
Update 2: Added extra conditional so it builds locally too.
Update 3: Corrected to use
NOW_REGIONoverNOWon now v2.@efleurine @TLevesque Where is
withCss/withSassgetting defined? It’s not included in your example.You must make sure you only
require('@zeit/next-css')after you’ve checkedif (phase === PHASE_PRODUCTION_SERVER).ie;
Don’t do this:
Do this
Update: added conditional so it will work when building locally too.
Update 2: Corrected to use
NOW_REGIONoverNOWon now v2.I’m not sure what the issue is 🤔 As per zeit/now-examples#214:
Or when you’re using plugins:
now-buildscript to package.json:You can read more about the serverless target here: https://github.com/zeit/now-builders/pull/150
And here: https://github.com/zeit/next.js/pull/5927
Then run
now. You can verify if you’re running the new serverless mode by checking/_logsforMODE: serverless. If it saysMODE: legacyit means your package.json has a Next.js version that doesn’t supporttarget: 'serverless'as per the specification here: https://github.com/zeit/now-builders/pull/150@alexedev, @carlesandres, @MaksKoseda: 😱 My apologies, I gave you a bum steer; the env var we should be checking against is
NOW_REGION(notNOW). I’ve updated the examples above, and copied here for posterity:The only change is:
Everyone in this issue will want to upgrade their application to this: https://github.com/zeit/now-examples/pull/214
package.json+ don’t drop yarn.lock/package-lock.json anymore. We don’t add next-server anymore etc.More details here: https://github.com/zeit/now-builders/pull/150
@jesstelford OMG I finally got this to work thanks to you!. Thanks a lot man!
@TLevesque Btw, I got mine to work with
next-compose-pluginswith:Worth noting, that considering this issue, the documentation here is incorrect then: https://www.npmjs.com/package/@zeit/next-typescript
@jesstelford thanks but I am still getting the error
by the way it should be
next/constantsmy code below but errors keep comming
As it is explained here no custom server can be used with lambdas deployed with now v2. Does it mean next-routes cannot be used? What about dynamic pages with clean urls?
Exactly 🙏 No workarounds and a lot of benefits 👍
@timneutkens ok great, so none of the workarounds above that we had to do are necessary anymore! Fantastic! Thanks you for your hard work
@jesstelford that change:
fixed my problem with deploying to now v2. Thanks!
Hi
Unfortunately, I still get that error. My config is:
Nope, same error on local building.
Edit: Ok, looks like the condition was wrong. This works:
Here is the content of the next.config.js file which is working properly on now v2, for a complex example with several plugins from Nextjs and its community, like dotenv, next-images, @zeit/next-sass, webpack-bundle-size-analyzer, @zeit/next-bundle-analyzer and next-plugin-transpile-modules. Here is the repo if it can help someone: https://github.com/TLevesque/platform-trials
@jesstelford, just to make sure, can you confirm that this is the correct syntax to use in term of best practices:
@efleurine Just for info, apparently next-compose-plugins package + next-sass doesn’t work with this pattern suggested by jesstelford : https://github.com/cyrilwanner/next-compose-plugins/issues/13
Example working migration: https://github.com/webpro/bookstore/commit/f5da96599095aa6b8d80330ab5b7ff7f2e2ae6fb per @timneutkens guide. Thanks, Tim!
This is the exact same problem I had when trying to directly implement
with-dotenvfrom the Next.JS examples.Thank you @webpro, @alexedev and @jesstelford !!! It worked perfectly. You don’t know how many hours I’ve wasted in this and how many more I would’ve wasted without you guys. Thanks!
An alternative to
dotenvas @alexedev describes isdotenv-webpack:Then you can use
process.env.*directly as it will be read from.envand replaced during build.well, I finally resolved my problem…
Lambda failed (error 502) because I relied on
dotenvpackage to take env values from.envfile but two things went wrong:.envis ignored bynowrequire('dotenv').config();is called in now environment (I did not understand the reason yet).Instead, env values should be referenced in
now.jsonthe values are becoming available automatically with no need to calldotenvand such see https://zeit.co/docs/v2/deployments/environment-variables-and-secrets/my
now.jsonlooks likeSo here is how the fixed
next.config.jslooks like:this can get a bit more readable by assigning
process.env.NOW_REGIONto a constant but I wanted to be explicitIf you have any other suggestions for more eloquent fix, please let me know
Just opened #5846 to discuss/address this issue
It looks good to me, and if it works, then I’d say you’re good to go 🎉
Thank you very much @jesstelford for your help!
For anyone stumbling into this, the issue is not necessarily related to next-css. I got the same problem having
withBundleAnalyzer(...)returned from next.config.js.Wrapping it the same way as described above helped.