This document describes the problem I faced as well as the solution
I need to access an ESM module from my output CJS project.
(I don't understand all of these terms well)
To do so, I need to use import
. It's the only way that'll work.
A regular import execa from 'execa'
doesn't work because Ocliff doesn't like this. It only likes import
.
So, I decided to use import
, but something weird happened...
➜ git:(prod) ✗ ns dev:apk:patch ./apk/aptoide/
Error: require() of ES Module
/root/project/node_modules/execa/index.js from
/root/project/lib/commands/dev/apk/patch.js not
supported.
Instead change the require of index.js in
/root/project/lib/commands/dev/apk/patch.js to a
dynamic import() which is available in all CommonJS modules.
Code: ERR_REQUIRE_ESM
Indeed, SWC was converting my import
to require
!!!
I found the option that asks SWC to not touch my dynamic imports.
It's called ignoreDynamic
.
Here is my .swcrc
for reference:
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"dynamicImport": true
},
"target": "es2022"
},
"module": {
"type": "commonjs",
"ignoreDynamic": true
}
}
Good luck on your journey!