esm icon indicating copy to clipboard operation
esm copied to clipboard

Using esm to import esm synchronously.

Open jakobrosenberg opened this issue 1 year ago • 1 comments

Is it possible to do something like this with esm to synchronously import esm modules?

import esm from 'esm'
import { createRequire } from "node:module";
const require = createRequire(import.meta.url)(esm)(module);


// synchronously import a file
const thing = require('./somefile.js')

jakobrosenberg avatar Dec 08 '22 22:12 jakobrosenberg

I found a work around by creating a .cjs file to handle the dynamic imports. Alas it struggles to import ESM files if the imported file doesn't have a .mjs extension. Setting package.json type to module in the package of the imported file seems to make no difference.

jakobrosenberg avatar Dec 09 '22 18:12 jakobrosenberg