Skip to content
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

Use explicit extensions in TypeScript's package.json for Node ES resolution compatibility #44342

Open
tsapko3628 opened this issue May 30, 2021 · 3 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@tsapko3628
Copy link

Bug Report

πŸ•— Version & Regression Information

Versions:

  • 4.3.2
  • 4.1.2
  • This is a crash
  • This is the behavior in every version I tried

⏯ Playground Link

Minimal repoducible example

πŸ’» Code

Run tsc (yarn tsc, yarn exec tsc, npx tsc, ./node_modules/.bin/tsc) with nodejs flag --experimental-specifier-resolution=node (NODE_OPTIONS="--experimental-specifier-resolution=node" tsc)

πŸ™ Actual behavior

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file:///path/to/project/node_modules/typescript/bin/tsc" 
    at Loader.getFormat (internal/modules/esm/loader.js:117:13)
    at async Loader.getModuleJob (internal/modules/esm/loader.js:242:20)
    at async Loader.import (internal/modules/esm/loader.js:176:17)
    at async Object.loadESM (internal/process/esm_loader.js:68:5) {
  code: 'ERR_INVALID_MODULE_SPECIFIER'
}

πŸ™‚ Expected behavior

Build without errors

@dansaper
Copy link

I assume this is related to https://gist.github.com/weswigham/22a064ffa961d5921077132ae2f8da78?

@tsapko3628
Copy link
Author

Not, it's not.
This problem because of bin script definition.
Bin scripts defined as

    {
        "tsc": "./bin/tsc.js",
        "tsserver": "./bin/tsserver"
    }

And when I call yarn tsc, nodejs trying to run node_modules/typescript/bin/tsc. This file hasn't extension and nodejs throws an error.
Why it works in Jest?
Because it use another bin declaration like this:

{
"jest": "./bin/jest.js"
}

For package manager, this script has link with name jest, but nodejs run jest.js, file with extension.
So, solving my problem is adding .js in bin files and in paths in package.json bin. It won't change behavior for exists project by help projects with configurations like mine.

As you can see, another experimental flags for native modules aren't change behavior (Now I run tsc without --experimental-specifier-resolution, but with all another flags in scripts and it works)

@tsapko3628
Copy link
Author

I created pull request with resolution. Can somebody approve it?

@DanielRosenwasser DanielRosenwasser changed the title Support node es modules resolution Use explicit extensions in TypeScript's package.json for Node ES resolution compatibility Jun 1, 2021
@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants