fix(js): CJS build with nodenext #110
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
In CommonJS projects that use
nodenext
ornode16
asmoduleResolution
, importing the@bonfida/spl-name-service
library is impossible. We encounter the following error:The issue is also described in #105.
Context
With
nodenext
andnode16
formoduleResolution
—which is now the value recommended by TypeScript asclassic
andndoe10
are deprecated—TypeScript interprets dist files dependending on their extension (.cjs
/.d.cts
/.mjs
/.d.mts
), or depending on the library'stype
(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 ismodule
.sns-sdk/js/package.json
Line 8 in 42a058f
This explains why TypeScript cannot load
@bonfida/spl-name-service
in a CommonJS project withnodenext
andnode16
formoduleResolution
.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")
Solution
To keep it simple, everything is now exported as
.js
/.d.ts
in their respective directory (dist/esm
anddist/csj
). For CommonJS, we create a simpledist/cjs/package.json
file that contains the following:; this way, both the source code and types are interpreted correctly.
Here is the result of arethetypeswrong after the change:

Reference
.ts
microsoft/TypeScript#49462 (comment)tsconfig.json
files and a specific CLI.