Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update .esm.js files to .mjs for Node 18 compatibility #163

Open
chriskirknielsen opened this issue Apr 24, 2024 · 0 comments
Open

Update .esm.js files to .mjs for Node 18 compatibility #163

chriskirknielsen opened this issue Apr 24, 2024 · 0 comments

Comments

@chriskirknielsen
Copy link

chriskirknielsen commented Apr 24, 2024

Hi there,

First off, this is a wonderful little library, great work!

I'm using ESM with Node 18 (same result on latest Node 22) and using import fails on import fetch from 'isomorphic-unfetch';. I've locally resolved this by renaming the psn-api.esm.js file to psn-api.mjs and adjusting the package.json accordingly.

Here's the original error, for context:

[Named export 'exchangeCodeForAccessToken' not found. The requested module 'psn-api' 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 'psn-api';
const { exchangeNpssoForCode, exchangeCodeForAccessToken, getUserTitles } = pkg;

And using that suggestion…

Cannot use import statement outside a module (via SyntaxError)
Original error stack trace: .../node_modules/psn-api/dist/psn-api.esm.js:1
import fetch from 'isomorphic-unfetch';
^^^^^^

I'm not sure how this is handled as it seems automated by the tsconfig.json file and I don't use TypeScript, so I'm a little out of my depth. However I imagine that perhaps it is an option set by compilerOptions.module, though looking at some documentation, this might be because this library supports older versions of Node (I see references to node16).

I'm trying to go "all in" on ESM in my project, and would love to be able to leverage this library without any weird hacks, but I understand if this would be a breaking change that cannot be implemented quickly.

I would also be happy to submit a pull request if somebody can point me in the right direction to push this along — or if my previous point about a breaking change is relevant, I'd probably fork this repo for my own use with the update, and patiently wait for an official release.

Let me know if I can help, and thank you!
Chris

EDIT: For now I will use this workaround:

import Module from 'node:module';
const require = Module.createRequire(import.meta.url);
const { exchangeNpssoForCode, exchangeCodeForAccessToken, getUserTitles } = require('psn-api');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant