问题
我有一个“commonjs”打字稿项目,我需要添加几个作为 ESM 包的 npm 依赖项。
我目前无法将我的项目从
type: 'commonjs'
移动到type: 'module
,并且我无法降级我需要的库的版本。
当我在导入 ESM 库后尝试构建/运行我的应用程序时,出现以下错误
Error [ERR_REQUIRE_ESM]: require() of ES Module <path to ESM library index.js> not supported.
Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.
问题
是否可以将ESM包导入到commonjs项目中?
尝试
我尝试了以下帖子中的一些解决方案,但似乎没有任何效果。我总是遇到同样的错误。
是否可以将ESM包导入到commonjs项目中?
是的,使用动态导入:
const somePackage = await import('package-name');