achievements-app / achievements-app/psn-api

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

Aberta
#163 4 comentários 4 reações 0 responsáveis Ver no GitHub
Linguagem predominante
TypeScript
Estrelas
422
Forks
56
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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](https://www.typescriptlang.org/docs/handbook/modules/reference.html#node16-nodenext), 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');
```

Guia de contribuição

Abrir o guia de contribuição

Direção de pesquisa

Start by reading `tsconfig.json` and `package.json`, since the request calls out module output settings and package wiring. Then inspect the built artifact from the error trace (`dist/psn-api.esm.js`) and related references to locate every place expecting `.esm.js`. Update the ESM entry to emit and expose `.mjs` and verify by running a Node 18+ `import` check that named imports from `psn-api` work without the `createRequire` workaround.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
node.js, typescript
Domínio
build-system
Tipo de issue
Bug
Dificuldade
2/5
Tempo estimado
1-3 horas
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
55/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.