2.0.2 • Published 2 years ago

fix-esm-candy v2.0.2

Weekly downloads
-
License
WTFPL OR CC0-1.0
Repository
github
Last release
2 years ago

fix-esm

If you're here, you've probably run into this kind of error:

internal/modules/cjs/loader.js:1015
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/user/projects/your-project/node_modules/some-module/lib/index.js
require() of ES modules is not supported.
require() of /home/user/projects/your-project/node_modules/some-module/lib/index.js from /home/user/projects/your-project/test-esm.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/user/projects/your-project/node_modules/some-module/package.json.

or this:

const _require = (0, _nodeModule.createRequire)(import.meta.url);
                                                       ^^^^

SyntaxError: Cannot use 'import.meta' outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)

This module fixes that. Some people will tell you that you need to switch to ESM to make it work, but unfortunately ES Modules are actually pretty terrible, as they do not support things like parametric modules, and they don't actually provide the benefits that many people claim they do. The resulting ecosystem split has unnecessarily caused a lot of misery for everybody.

So, instead, you can use this library to continue using CommonJS in your code, while also being able to use newer ESM releases of libraries! That means there's compatibility both ways again.

How does it work?

Basically, it wraps the require function, detects an ESM import failure, and then uses Babel to do a very limited transpilation. It doesn't change any of the actual code (and so it shouldn't break anything), it only changes the import/export syntax to make it work with CommonJS.

Since the code only triggers when you hit an ESM import error, the impact on performance should be minimal - it's not like @babel/register, which would typically transpile all of your code.

Is it reliable?

Probably. To be clear: this library uses a somewhat hacky approach, and it might break in the future. But since many people use Babel for browser code anyway, most every ESM module is going to work with Babel's module transforms, and so this should work fine too. The biggest risk is in Node's module system internals changing in such a way that it breaks the patching, but this is not that likely.

How do I use it?

There are two main ways to use this library; either by using the custom require function it exports, or by setting it up globally.

Custom require

First off, there's the 'custom require' approach. This is the approach you should use when using fix-esm-candy in a library you intend to publish! It's bad practice to publish libraries that modify things globally, and the 'custom require' approach ensures that you don't override anything global.

Simply change your require like this:

// From...
const someModule = require("some-module");
// ... into ...
const someModule = require("fix-esm").require("some-module");

That's it! The require will now work.

Global hook

If you're building an application, then it will probably be more practical to register a handler globally. You should do this at the start of the entry point file, such as your app.js or server.js, before loading any other modules, but after any other tools that set global require hooks (such as @babel/register).

Like so:

require("fix-esm").register();

const someModule = require("some-module");
// More requires go here...

That's it! Now every require throughout your application will work, even with ESM modules.

Changelog

2.0.0 (October 10, 2022)

  • Add support import.meta.url

1.0.1 (August 21, 2021) (original)

  • Fixed e-mail link in README.

1.0.0 (August 21, 2021) (original)

Initial release.