fix(js): CJS build with nodenext #110
Merged
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
nodenextornode16asmoduleResolution, importing the@bonfida/spl-name-servicelibrary is impossible. We encounter the following error:The issue is also described in #105.
Context
With
nodenextandnode16formoduleResolution—which is now the value recommended by TypeScript asclassicandndoe10are 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.tsdist files.Cause
@bonfida/spl-name-serviceexports 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-servicein a CommonJS project withnodenextandnode16formoduleResolution.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.tsin their respective directory (dist/esmanddist/csj). For CommonJS, we create a simpledist/cjs/package.jsonfile 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:

Reference
.tsmicrosoft/TypeScript#49462 (comment)tsconfig.jsonfiles and a specific CLI.