Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Library cannot be used with modern moduleResolution TypeScript settings #386

Closed
1 of 3 tasks
akwodkiewicz opened this issue Feb 5, 2024 · 4 comments · Fixed by #387
Closed
1 of 3 tasks

Library cannot be used with modern moduleResolution TypeScript settings #386

akwodkiewicz opened this issue Feb 5, 2024 · 4 comments · Fixed by #387
Labels
bug Something isn't working

Comments

@akwodkiewicz
Copy link
Contributor

akwodkiewicz commented Feb 5, 2024

Description

It is not possible to import any type exported internally from the lib directory when your TS project is set to any of the modern values of moduleResolution: "Node16", "NodeNext" or "Bundler"

Explanation

The reason for that is filename clash between ./dist/lib.js and ./dist/lib/ directory -- modern resolution algorithms resolve the './lib' in

export * from './lib'

in the ./dist/index.d.ts file to the ./dist/lib.js , not ./dist/lib/index.d.ts, because that's how the Node resolution algorithm works (lib.js has a higher precedence than lib/index.js).

The quickest way to resolve the issue is to change the lib.js filename to something like lib.web.js, so that there is no confusion between the file name and the directory name.

Reproduction

Prerequisites

  • A TS Project with a compilerOption "moduleResolution" set to "Node16", "NodeNext" or "Bundler"

Steps to reproduce

  1. Try to import things like
import { CasperClient, CLPublicKey, DeployUtil } from 'casper-js-sdk';

Expected behavior

The import is correct.

Actual behavior

TypeScript error 2305:

Module '"casper-js-sdk"' has no exported member 'DeployUtil'

Screenshots

Screenshot 2024-02-05 at 11 15 30 Screenshot 2024-02-05 at 11 15 35

Additional context

The bug does not have any runtime implications, but it's not possible to import the library correctly in projects relying on current TypeScript defaults.

Defect Type

  • Regression
  • New
  • Escape
@akwodkiewicz akwodkiewicz added the bug Something isn't working label Feb 5, 2024
@akwodkiewicz
Copy link
Contributor Author

I'll try to address that in a PR in a moment

@akwodkiewicz
Copy link
Contributor Author

akwodkiewicz commented Feb 5, 2024

A workaround for Node devs is to remove the dist/lib.js file from the package after installation -- Node is going to import dist/lib.node.js, as defined in package.json#node, and TypeScript will start resolving import from ./lib to dist/lib/index.d.ts.

@hoffmannjan
Copy link
Contributor

Fixed in release-2.15.4.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@hoffmannjan @akwodkiewicz and others