standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 147 forks source link

ERR_REQUIRE_ESM: The simplest fix! #897

Open michaelfig opened 3 years ago

michaelfig commented 3 years ago

The simplest way I could find to fix #868 was to publish a fork.

Please take a look at https://github.com/agoric-labs/esm/tree/Agoric#readme

Venryx commented 3 years ago

Didn't work for me; not sure why.

I installed the modified esm package using Yarn 2: (note that the instructions in your readme don't work for Yarn 2; I had to add esm@ to the start, as instructed here)

yarn add -D esm@https://github.com/agoric-labs/esm.git#Agoric-built

I added this as my launch script:

node -r esm ./dist/index.js

However, when I run that script, it still gives the same error as the regular esm:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\...\dist\index.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1217:13) {
  code: 'ERR_REQUIRE_ESM'
}

If I find further info, I'll post it here.

Venryx commented 3 years ago

Oh, it looks like Node 14.13.0+ supports named imports from common-js modules!: https://simonplend.com/node-js-now-supports-named-imports-from-commonjs-modules-but-what-does-that-mean

Well, some of them anyway. (the blog post linked shows how it only works for some packages -- ones where cjs-module-lexer is able to detect the export names through static analysis)

There may not be a need for these sorts of workarounds soon then. (I'll have to see, after using Node 14.13.0+ for a while)

motss commented 3 years ago

@michaelfig Thanks for the simple fix. I also found out that the fix itself is not enough, at least for my case. I have to override the esm config with the following config:

/** package.json */
{
  ...
  "esm": {
    "cjs": true,
    "mode": "all"
  }
}
mashpie commented 2 years ago

@michaelfig any plans to release your fix to npm or get this merged into esm?

It's providing exactly what I wanted: Authoring ESM modules by default with .js extentension. Use a index.cjs as entry for require() - perfect upgrade path imho. Thanks a lot!

michaelfig commented 2 years ago

@michaelfig any plans to release your fix to npm or get this merged into esm?

Doesn't seem so. I think this upstream esm repo has been abandoned.

Also, instead of putting on NPM (where it would need to have a different name), just follow the instructions at the top of the readme linked in the description to install from Github.

Thanks a lot!

My pleasure!

mk-pmb commented 2 years ago

@michaelfig Do you still maintain the agoric fork? I get lots of deprecation warnings and, more importantly, a test failure. I posted them there because the agoric repo has its issue tracker disabled. Would you nonetheless be willing to debug them?

michaelfig commented 2 years ago

@michaelfig Do you still maintain the agoric fork? I get lots of deprecation warnings and, more importantly, a test failure. I posted them there because the agoric repo has its issue tracker disabled. Would you nonetheless be willing to debug them?

Sorry, I don't need this package anymore as Agoric has completed its migration off of it, and other priorities beckon.

I hate to contribute to more abandonware, but here I am.

mk-pmb commented 2 years ago

Ok then. Could you explain that in your README then? Thanks!

mk-pmb commented 2 years ago

@mashpie or anyone else who'd prefer to install it from npm rather than GitHub: https://www.npmjs.com/package/agoric-labs-esm-built-pmb