4 ms·
I have a ton of projects and a huge amount of utilities and such, that all use CJS. You cannot just use 'import' if you also use CJS. To use 'import', it must
by tqwhite 2y ago
I have a ton of projects and a huge amount of utilities and such, that all use CJS.
You cannot just use 'import' if you also use CJS. To use 'import', it must be an ESM module and the you cannot use require() to access CJS utilities.
In CJS code, one can use import() to bring in ESM modules but, import() is asynchronous. The problem is that I have a lot of code that is not asynchronous. To add an asynchronous function requires substantial rewriting that I am rarely willing to do.
If, in addition to the asynchronous import(), there was a synchronous importSync(), I could use ESM modules any time I want. That would allow me to...
1) Use and support new utilities that are ESM.
2) Write new utilities that are ESM. Presently, I always target CJS because that's what I have to use.
I have read that there is a new version of NodeJS that supports using require() for ESM modules. That will be a huge, huge, huge improvement although I would prefer importSync() so that it was clear when I was using an ESM module. Even so, I will take it.