mhart/aws4fetch

regression 1.0.16: not possible to use in ESM build and nodejs

Closed this issue · 8 comments

the change in bc5adb8 gives the following error in esm builds:

import { AwsV4Signer } from 'aws4fetch';
...
file:///Users/nobody/codez/test/src/utils.js:18
import { AwsV4Signer } from 'aws4fetch';
         ^^^^^^^^^^^
SyntaxError: Named export 'AwsV4Signer' not found. The requested module 'aws4fetch' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'aws4fetch';
const { AwsV4Signer } = pkg;

changing it to the CJS format produces:

import aws4fetch from 'aws4fetch';
const { AwsV4Signer } = aws4fetch;
(node:63327) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

/Users/nobody/codez/test/node_modules/aws4fetch/dist/aws4fetch.esm.js:279
export { AwsClient, AwsV4Signer };
^^^^^^

SyntaxError: Unexpected token 'export'

Renaming the ems files to .mjs solves the issue:

  "exports": {
    ".": {
      "import": "./dist/aws4fetch.esm.mjs",
      "worker": "./dist/aws4fetch.esm.mjs",
      "browser": "./dist/aws4fetch.umd.js",
      "require": "./dist/aws4fetch.cjs.js",
      "default": "./dist/aws4fetch.umd.js"
    }
  },
mhart commented

esm builds using what tool? It seems to work fine in esbuild

mhart commented

(also import aws4fetch from 'aws4fetch' is not CJS – CJS uses require)

esm builds using what tool? It seems to work fine in esbuild

just pure node 16

(also import aws4fetch from 'aws4fetch' is not CJS – CJS uses require)

yes. but this is the problem. node thinks that the ./dist/aws4fetch.esm.js is a CJS module, which it isn't.

minimal project: https://github.com/tripodsan/aws4fetch-test

$ node -v
v16.17.0

$ node index.js
file:///Users/nobody/codez/foo/index.js:1
import { AwsV4Signer } from 'aws4fetch';
         ^^^^^^^^^^^
SyntaxError: Named export 'AwsV4Signer' not found. The requested module 'aws4fetch' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'aws4fetch';
const { AwsV4Signer } = pkg;
mhart commented

Oh interesting – I didn't know anyone was using this directly from Node.js with ESM. When I first created this, Node.js didn't have fetch support, so importing from Node.js as an ESM module wasn't even a consideration.

I'll need to look into the best way to do this. There's the main/module/browser declarations at the top level of package.json and then all the exports declarations as well.

Best option might just be to have an mjs and a js version (for backwards compat)

mhart commented

Try out 1.0.17 and let me know how it goes

Try out 1.0.17 and let me know how it goes

perfect. works!

thanks for the quick turnaround