I have a Node.js project whose modules are imported in the Common.JS way using require
. However, the need arose to import a package that is ESM only.
One way to use ESM import from
in my project was to add "type": "module"
to my package.json
file. However, all the requires
stoped working.
I verified that one solution would be to create the require module from the module
package:
import { createRequire } from "module";
const require = createRequire(import.meta.url);
However, the entire (large) project has imports with require
and it seems to me unfeasible to have to add this solution to each one of them.
So I'd like to know if there's an elegant way to declare require
just once and have it be used throughout the project.
Or, is there any more elegant solution than creating the require
or substituting everything for ESM?