Skip to content

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

Open

Description

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

Activity

  1. dansaper commented on May 31, 2021

    @dansaper
  2. tsapko3628 commented on May 31, 2021

    @tsapko3628
    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)

  3. tsapko3628 commented on Jun 1, 2021

    @tsapko3628
    Author

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

  4. changed the title [-]Support node es modules resolution[/-] [+]Use explicit extensions in TypeScript's package.json for Node ES resolution compatibility[/+] on Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions