achievements-app / psn-api

A JavaScript library that lets you get trophy, user, and game data from the PlayStation Network.
https://psn-api.achievements.app
MIT License
263 stars 31 forks source link

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

Open chriskirknielsen opened 4 months ago

chriskirknielsen commented 4 months ago

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');