解决nextjs部署到now上之后出现的“Unable to import module 'now__launcher'”错误

解决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

 

posted on   axel10  阅读(678)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示