将模块导入下一个js

Import modules into next js

实际上我有一个项目需要作为模块包含在 Next JS 中。所以基本上我试图通过 npm link 使用它,我成功地能够将该模块作为组件添加到我的下一个 JS 存储库中。但由于 webpack 加载程序,它正在中断。我是否需要在下一个 JS 中添加所有必要的加载程序?实现此目标的更好方法是什么?

默认情况下 Next.js 不编译位于 node_modules 的依赖项。

您应该使用 next-transpile-modules 以指示它编译特定的库。

// next.config.js
const withTM = require('next-transpile-modules')(['somemodule', 'and-another']);

module.exports = withTM({
  ...regular next config
});

随着 Next 的新更新,js 支持 ES 模块和 URL 导入。

Starting with Next.js 11.1, we added experimental support for ES modules being prioritized over CommonJS modules. In Next.js 12, this is now the default. Importing NPM modules that only provide CommonJS is still supported.

https://nextjs.org/blog/next-12#es-modules-support-and-url-imports