简体   繁体   中英

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. According to the Node documentation , the standard way to do this is using the 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:

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?

check that in the package.json what have you selected as the option. Make sure to select type:"common.js" instead of "module"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM