Skip to content

fix(js): CJS build with nodenext #110

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexandre-abrioux
Copy link

@alexandre-abrioux alexandre-abrioux commented Jun 6, 2025

Issue

In CommonJS projects that use nodenext or node16 as moduleResolution, importing the @bonfida/spl-name-service library is impossible. We encounter the following error:

error TS2305: Module '"@bonfida/spl-name-service"' has no exported member [... name of the module ...]

The issue is also described in #105.

Context

With nodenext and node16 for moduleResolution—which is now the value recommended by TypeScript as classic and ndoe10 are deprecated—TypeScript interprets dist files dependending on their extension (.cjs/.d.cts/.mjs/.d.mts), or depending on the library's type (commonjs/module) for .js/.d.ts dist files.

Cause

@bonfida/spl-name-service exports JavaScript files with correct extensions (.cjs/.mjs), but the types are only exported as .d.ts, so can only be interpreted as ESM because the library's type is module.

"type": "module",

This explains why TypeScript cannot load @bonfida/spl-name-service in a CommonJS project with nodenext and node16 for moduleResolution.

You can verify the issue by using the arethetypeswrong tool available at this URL:
https://arethetypeswrong.github.io/?p=%40bonfida%2Fspl-name-service%403.0.11
(see the "Masquerading as ESM warning")

image

Solution

To keep it simple, everything is now exported as .js/.d.ts in their respective directory (dist/esm and dist/csj). For CommonJS, we create a simple dist/cjs/package.json file that contains the following:

{
  "type": "commonjs"
}

; this way, both the source code and types are interpreted correctly.

Here is the result of arethetypeswrong after the change:
image

Reference

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

Successfully merging this pull request may close these issues.

1 participant