[英]Importing ES6 modules in CommonJS Node app?
Long story short, I am trying to use an npm package that only has an ES6 module entrypoint, namely the bids-validator
( GitHub ), in a CommonJS module as my entire 50+ module app is built in CommonJS.长话短说,我正在尝试使用只有一个 ES6 模块入口点的 npm package,即
bids-validator
( GitHub0 + 内置应用程序作为我的整个 JS 通用模块) According to the Node documentation , the standard way to do this is using the import()
function.根据节点文档,执行此操作的标准方法是使用
import()
function。 I tried this approach, with the following code:我尝试了这种方法,使用以下代码:
async function importValidator() {
const { validate } = await import('bids-validator')
return validate
}
const validate = importValidator()
However, this results in an error from the bids-validator
package, not necessarily the import function above:但是,这会导致来自
bids-validator
package 的错误,不一定是上面的导入 function:
error: unhandledRejection: Cannot use import statement outside a module
/*/app/api/node_modules/bids-validator/index.js:2
import validate from './validators'
Is there a way to circumvent this, so that I could use the package within a CommonJS module?有没有办法避免这种情况,以便我可以在 CommonJS 模块中使用 package ?
check that in the package.json what have you selected as the option.检查 package.json 中您选择了什么作为选项。 Make sure to select type:"common.js" instead of "module"
确保 select 类型:“common.js”而不是“模块”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.