Nextjs SyntaxError: Cannot use import statement outside a module错误

发布时间 2023-12-27 17:02:42作者: unfetteredman

NextJs 报 SyntaxError: Cannot use import statement outside a module 第三方依赖不能导入问题

 

解决方案:

1,Next.JS13.1+,可以使用next.config.js中的属性transpilePackages

const nextConfig = {
  transpilePackages: ['the-npm-package'], // 第三方的依赖
};
module.exports = nextConfig;

2,next-transpile-modules 一个NPM模块,它允许您指定要传输的模块。

// next.config.js
const withTM = require('next-transpile-modules')(['somemodule', 'and-another']); // pass the modules you would like to see transpiled

module.exports = withTM(withLess({
  ... // your custom next config
}));