解決nextjs部署到now上以後出現的「Unable to import module 'now__launcher'」錯誤

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

相關文章
相關標籤/搜索