Last active
August 31, 2021 21:56
-
-
Save mkg20001/59b74ea2bd1ae66fc904d9422785e94e to your computer and use it in GitHub Desktop.
Common things from CJS brought to ESM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
NOTE: | |
- The metadata of the current module "module" in ESM is under "import.meta" | |
- Dynamic import is always async, meaning you need to await it | |
- Also if you export default it's under .default, no longer returned directly | |
*/ | |
// dirname | |
import { dirname } from 'path' | |
import { fileURLToPath } from 'url' | |
const __dirname = dirname(fileURLToPath(import.meta.url)) | |
// require | |
async function require (thing) { | |
const imported = await import(thing) // "dynamic import" | |
return Object.assign(imported.default || {}, imported) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment