Skip to content

Figure out how to deal with annoying node: prefix in auto-imports from @types/node #43842

Closed

Description

Nowadays @types/node ships with built-ins like fs as well as node:fs. Accidentally auto-importing these are extremely annoying if you're trying to target older versions of Node.js.

We should consider

  • inferring the best type of auto-import from your project
  • pick the older style of imports one until Node 16 14 becomes the last supported version
  • provide an option

CC Andrew Branch (@andrewbranch) Matt Bierner (@mjbvz)

Activity

  1. andrewbranch commented on Apr 27, 2021

    @andrewbranch
    Member

    Thanks for opening an issue—this has been on my mental to-do list since Ben Lichtman (@uniqueiniquity) brought it up. I’m actually surprised we haven’t gotten a flood of complaints about it already 🤔

  2. jakebailey commented on Apr 27, 2021

    @jakebailey
    Member

    I think it's very likely that people just have old versions of the packages installed, predating DefinitelyTyped/DefinitelyTyped#51107.

    I know I hit this and it's frustrating, but I'm pretty diligent about updating my stuff regularly.

  3. jakebailey commented on Apr 27, 2021

    @jakebailey
    Member

    And, this may go away in short order with DefinitelyTyped/DefinitelyTyped#52595.

  4. DanielRosenwasser commented on Apr 28, 2021

    @DanielRosenwasser
    MemberAuthor

    CC Ron Buckton (@rbuckton) we don't really understand this well enough, and I heard there's some churn going into DefinitelyTyped?

  5. andrewbranch commented on Apr 28, 2021

    @andrewbranch
    Member

    #42764 for reference

  6. sheetalkamat commented on Apr 28, 2021

    @sheetalkamat
    Member
  7. karlhorky commented on May 7, 2021

    @karlhorky
    Contributor

    It seems like the PR from Simon Schick (@SimonSchick) was merged DefinitelyTyped/DefinitelyTyped#52595

    Unfortunately, some applications (like ours) are depending on these module specifiers to work in TypeScript and also VS Code: DefinitelyTyped/DefinitelyTyped#52595

    Could the fix in #42764 be prioritized so that node: prefixes stay valid module specifiers?

  8. SimonSchick commented on May 7, 2021

    @SimonSchick
    Contributor

    Even if #42764 was fixed we would not be able to use this feature as the typings need to support old versions.

  9. karlhorky commented on May 8, 2021

    @karlhorky
    Contributor

    I guess what I would hope for (although I'm not sure if this is the changes described in #42764) would be to satisfy both requirements (prefixed and unprefixed builtin module specifiers):

    1. the node: definitions stay in to not break users who are using them
    2. the non-prefixed versions also stay there
    3. the non-prefixed versions are selected for auto-imports (no user configuration for selecting the node: versions for now)

    Also mentioned that over here: DefinitelyTyped/DefinitelyTyped#52595 (comment)

  10. andrewbranch commented on May 10, 2021

    @andrewbranch
    Member

    Even if #42764 was fixed we would not be able to use this feature as the typings need to support old versions.

    Simon Schick (@SimonSchick) you’re referring to the node: protocol imports not being supported in CJS require prior to Node 16, right? But if/when that gets backported to Node 14, or when you start writing new types for Node 16, you’d be able to use the feature at that point, right?

  11. SimonSchick commented on May 10, 2021

    @SimonSchick
    Contributor

    No, I'm talking about the fact that node typings lag several TS versions behind for compatibility.

  12. karlhorky commented on May 30, 2021

    @karlhorky
    Contributor

    So 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

  13. karlhorky commented on May 30, 2021

    @karlhorky
    Contributor

    Ah, maybe require / CommonJS support is not quite there yet for v14 and v12:

    In Node 16 require('node:fs') works, but it looks like it still doesn't in 14.17.0.

    DefinitelyTyped/DefinitelyTyped#52595 (comment)

  14. karlhorky commented on Jul 3, 2021

    @karlhorky
    Contributor

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

  15. Pytal commented on Aug 14, 2021

    @Pytal

    As the node: import URL scheme has been backported to v14.13.1 and v12.20.0 https://nodejs.org/api/esm.html#esm_node_imports

    image

    Will @types/node for versions 14 and 12 be updated?

  16. SimonSchick commented on Aug 14, 2021

    @SimonSchick
    Contributor

    I probably won't do it as I prefer working on the bleeding edge stuff, but the process is relatively trivial.

    • Add a secondary declare module for each module declaration that re-exports everything with the node: prefix
    • (Optional): Change all internal imports to prefer node: prefix.

    You can use the current typings as an example or even just copy/paste from there.

  17. ExE-Boss commented on Aug 14, 2021

    @ExE-Boss
    Contributor

    Pytal

    As the node: import URL scheme has been backported to v14.13.1 and v12.20.0

    That’s only in ECMAScript Module import statements, not the CommonJS require(…) function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions