Skip to content

--module nodenext self-name references don't work with an outDir from clean build #46762

Closed

Description

From Benjie (@benjie) at #44501 (comment):

Hello, your beta announcements are asking for feedback on this feature. First of all - I'm super excited about this! Great work!

One thing I was really looking forward to in Node.js exports/ESM support was having example files that import from the library itself - this way we can just say import { foo } from "my-module" rather than import { foo } from ".." /* TODO: replace this with "my-module" */ - makes it much easier for users to copy/paste examples whilst keeping the examples co-located inside the package.

Node.js supports this "self-referencing" behaviour; however TypeScript doesn't seem to understand how to import this in module:"nodenext" mode - a could not find a declaration file for module 'my-module' error is raised.

I've prepared a minimal reproduction here with js folder working using Node.js and analogous ts folder which is not working with TypeScript (won't compile):

https://github.com/benjie/typescript-self-referencing-issue

TSConfig:

{
  "compilerOptions": {
    "lib": ["es2021"],
    "module": "nodenext",
    "target": "es2021",

    "strict": true,

    "rootDir": "src",
    "declaration": true,
    "declarationDir": "dist",
    "outDir": "dist"
  },
  "include": ["src"],
  "exclude": ["dist"]
}

TypeScript version 4.6.0-dev.20211110

Maybe this relates to #46212?

Thanks for your hard work on TypeScript ❤️

Originally posted by Benjie (@benjie) in #44501 (comment)

Activity

  1. andrewbranch commented on Nov 10, 2021

    @andrewbranch
    MemberAuthor

    The package.json here is

    {
      "name": "@localrepo/ts",
      "type": "module",
      "exports": {
        ".": "./dist/index.js"
      },
      "dependencies": {
        "@types/node": "^16.11.7",
        "typescript": "^4.6.0-dev.20211110"
      }
    }

    The self-name reference works but resolves to ./dist/index.d.ts, which means it’s always going to be one build stale, and a clean build will not be able to resolve anything.

  2. Offirmo commented on Apr 28, 2022

    @Offirmo

    Hi, this is definitely a problem for enabling module self-reference as advertised in TypeScript 4.5, cf. this situation: #38675 (comment)

    Thanks for the fix and hopefully we'll get it soon!

  3. sheetalkamat commented on Apr 29, 2022

    @sheetalkamat
    Member

    Don’t know if this was discussed but this almost feels like project reference to ourself with using source files instead of .d.ts files and mimicking that they are present for module resolution like we do through tsserver .. may be a solution could be to use that for own tsconfig ?

  4. weswigham commented on Apr 29, 2022

    @weswigham
    Member

    Maybe as a workaround, but that's a bit far from first-class support for what is now a core node resolution feature.

  5. Offirmo commented on May 6, 2022

    @Offirmo

    Thanks a lot Wesley Wigham (@weswigham) I can't wait to try it!

  6. benjie commented on May 20, 2022

    @benjie

    Thanks for this Wesley Wigham (@weswigham) 🙌 ❤️

    The fix/feature is out in typescript@rc (4.7.1-rc) and seems to be working well so far 🚀

  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions