I decided to completely drop CommonJS from January 2022.
All my modules will only support Node.js v14 and v16 beginning from January 2022.
New Packages will only support Node.js v14 and v16 from the start.
Like so:
// package.json
{
"type": "module",
"engines": {
"node": "^14.15 || >=16"
}
}
// .npmrc
engine-strict=true
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "es2020",
"moduleResolution": "node",
"rootDir": "src",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"newLine": "lf"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
import { dirname } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
Yes, it is possible using Dynamic Imports
.
Example:
// index.cjs
const main = async () => {
const esmPackage = await import('esm-package')
// or maybe
const { module } = await import('esm-package')
// now use the esm-package like you are used to
}
main()
I will probably add the badge below to ESModule-Only packages: