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

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

Closed
andrewbranch opened this issue Nov 10, 2021 · 6 comments · Fixed by #47925
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@andrewbranch
Copy link
Member

andrewbranch commented Nov 10, 2021

From @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 in #44501 (comment)

@andrewbranch
Copy link
Member Author

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.

@Offirmo
Copy link

Offirmo commented Apr 28, 2022

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!

@sheetalkamat
Copy link
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 ?

@weswigham
Copy link
Member

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

@Offirmo
Copy link

Offirmo commented May 6, 2022

Thanks a lot @weswigham I can't wait to try it!

@benjie
Copy link

benjie commented May 20, 2022

Thanks for this @weswigham 🙌 ❤️

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants