解决nextjs部署到now上以后出现的“Unable to import module 'now__launcher'”错误css
这个错误是因为在next.config.js中直接引用了withLess之类的插件致使的。在now环境下require插件须要在PHASE_PRODUCTION_SERVER阶段下,若是不加这个阶段的判断就会报错。git
这个是错误的作法github
// ❌ Don't put this hereui
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(); };
正确的写法:this
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/5750spa