Skip to content

Consider allowing packages to define multiple canonical names for their modules #42764

Open

Description

@rbuckton

Suggestion

This is based on a change in the NodeJS definitions: DefinitelyTyped/DefinitelyTyped#51107. For Node's ESM support, you can import NodeJS core packages (i.e., fs, path, etc.) using a fully-qualified URN with the node: prefix. The approach taken in DefinitelyTyped/DefinitelyTyped#51107 is to re-export a module like fs using another ambient module:

declare module "node:fs" {
  export * from "fs";
}
declare module "fs" {
 ...
}

However, this belies the actual behavior of the import. "node:fs" is not a different module that happens to re-export "fs", it is actually the same module, and ideally would be handled in a way similar to how we currently handle symbolic links. With the above definitions, any module augmentations must always be made against the non-prefixed form of the module. Augmentations of the prefixed-form would be missing when importing the non-prefixed form.

We already have some mechanisms for handling this in your own project (i.e., via "paths", etc.), but not when importing from an external package (such as is the case with "fs", "path", etc.).

Should we consider adding a mechanism, be it either via syntax or via configuration (stored in package.json)?

Syntax

We can bikeshed on syntax, but I've presented a few options below:

// 1) variation of `export as namespace ...`
declare module "fs" {
  export as module "node:fs"; // essentially a symbolic link from `"node:fs"` to `"fs"`
}

// 2) declare a module with multiple names
declare module "fs", "node:fs" {
}

Configuration

If we consider approaching this problem from a configuration standpoint there are a number of directions we could take. I can update this section as we discuss this issue.

🔍 Search Terms

NodeJS, ESM, prefix, symlink

Activity

  1. SimonSchick commented on Apr 1, 2021

    @SimonSchick
    Contributor

    Also should consider which module name takes precidence when automatically importing, probably want to use declaration order if we go with the declare module "fs", "node:fs" syntax, or have some sort of override.

  2. rbuckton commented on May 7, 2021

    @rbuckton
    ContributorAuthor

    A separate option I've considered is adding a new node-esm package to DefinitelyTyped that merely re-exports the node core packages with node:* names, though such a package would likely be short-lived.

  3. karlhorky commented on May 30, 2021

    @karlhorky
    Contributor

    As a side note regarding the node: protocol, looks like the node: scheme / prefix was backported to Node.js 14.13.1 / 12.20.0 🎉

    https://nodejs.org/api/esm.html -> link "node: Imports" (links with anchors don't scroll properly right now)

    Screen Shot 2021-05-30 at 11 04 39

  4. karlhorky commented on Jul 3, 2021

    @karlhorky
    Contributor

    @types/node@16.0.0 has been published now - the node: schema / prefix now works again:

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