解決nextjs部署到now上之後出現的“Unable to import module 'now__launcher'”錯誤
阿新 • • 發佈:2019-01-12
解決nextjs部署到now上之後出現的“Unable to import module 'now__launcher'”錯誤
這個錯誤是由於在next.config.js中直接引用了withLess之類的外掛導致的。在now環境下require外掛需要在PHASE_PRODUCTION_SERVER階段下,如果不加這個階段的判斷就會報錯。
這個是錯誤的做法
// ❌ Don't put this here
const withCSS = require('@zeit/next-css'); // 由於不在PHASE_PRODUCTION_SERVER階段所以報錯 const { PHASE_PRODUCTION_SERVER }= process.env.NODE_ENV === 'development' ? {} : !process.env.NOW_REGION ? require('next/constants') : require('next-server/constants'); module.exports = (phase, { defaultConfig }) => { if (phase === PHASE_PRODUCTION_SERVER) { // Config used to run in production. return{}; } return withCSS(); };
正確的寫法:
const { PHASE_PRODUCTION_SERVER } = process.env.NODE_ENV === 'development' ? {} : !process.env.NOW_REGION ? require('next/constants') : require('next-server/constants'); module.exports = (phase, { defaultConfig }) => {if (phase === PHASE_PRODUCTION_SERVER) { // Config used to run in production. return {}; } // ✅ Put the require call here. const withCSS = require('@zeit/next-css'); return withCSS(); };
參考:https://github.com/zeit/next.js/issues/5750