Closed DanielRosenwasser closed 1 year ago
I think this issue is a good example for the long needed plugin (hook) system.
The Solution to the first 2 Problems is rollup at present you can use it with plugin typescript to resolve anything correct and then inject it into the typescript program.
i am already researching how i could maintain and release a typescript-rollup version which would be typescript + rollup hooks and plugins.
a Plugin/Hook System is the Solution for the Resolve Problem. The Only one that is flexible and adjustable enough to cover every case.
Conclusion
a Plugin/Hook System is the Solution for the Resolve Problem. The Only one that is flexible and adjustable enough to cover every case.
There is already a hooks system built into package.json... it's bad, but the whole point is to get rid of it as soon as dependencies merge the PR's to fix the issues.
I have the following install hook as a bandaid while upstream applies the fixes to default imports and reachable types/modules.
#!/bin/bash
set -euo pipefail
sed -i '2s/import express/import \* as express/' node_modules/\@feathersjs/express/index.d.ts
sed -i '1s/import http/import \* as http/' node_modules/\@feathersjs/socketio/index.d.ts
sed -i '2s/import io/import \* as io/' node_modules/\@feathersjs/socketio/index.d.ts
sed -i '8s/"source",/"\.\/source\/index\.js",\n"types": "\.\/index\.d\.ts",/' node_modules/chalk/package.json
I'd rather have 4.5 stable sooner, than to wait for yet another workaround.
@rayfoss we can take your example to again show that a plugin/hook system like the one from rollup is badly neeeded.
you mixed linux shell script into your package.json as workaround.
Now we have an ecosystem mismatch between Node.js and bundlers. No bundler is compatible with this new TypeScript mode (and keep in mind, back-end code also occasionally uses a bundler). Is our guidance to always use this mode for plain Node.js apps, and let tools "catch up"?
Yeah, so the trouble is if TypeScript doesn't encourage the use of node12
/nodenext
then the package author will be unable to use any packages that use Node ESM. So no matter which choice the author makes some things will be invariably broken.
This is something I've mentioned in the past on some related issue, but has it been considered not to have a distinct node
and node12
/nodenext
mode at all, but rather just use --module node
and require the presence of "type": "module"
or "type": "commonjs"
in package.json
? (And regardless of this being present, .mts
/.mjs
/.cts
/.cjs
would work always, this would only be required for using .ts
/.js
in the new mode).
By having both --module nodenext
and "type": "module"
people are gonna be essentially double-configuring in a lot of cases anyway (most cases?), given people have to add --module nodenext
to their tsconfig
to enter the mode anyway, I don't see that it would be significantly worse to have them add "type": "module"/"commonjs"
to their package.json
instead.
Conclusion
a Plugin/Hook System is the Solution for the Resolve Problem. The Only one that is flexible and adjustable enough to cover every case.
This seems like the absolute worst conclusion to ever reach. I'm really sorry for butting in on this issue as not a huge avid typescript users but the one thing I like about typescript is precisely it doesn't require 9001 packages like soo many other builders and bundlers to actually get into a working state.
Typescript is standalone that just-works and doesn't require the end programmer to have to build-their-own-compiler themselves. Add plugins is just gonna be that, making it more and more complicated while adding nothing really. Especially something like this issue that REALLY needs to work out of the box but isn't. And saying to people "oh you're using typescript but typescript is dumb like bundlers, you need to hook plugins to get it working" is something you don't want to say to developers or people.
I really strongly advice against any case of adding plugins to this package. If it doesn't work, we can fix it. If it works, why would you need a plugin just to make configging even more complicated?
Best regards: TT
P.S. what is it with developers and wanting plugins in literally everything?
@TheThing in my case it is simply needed because there are many package authors with total diffrent opinions and i do not want to get blocked by them. I also do not want to hardfork everything and so on.
The only Alternativ to a plugin system in typescript is the usage of dev bundels that are typescript compatible.
also my conclusion is driven from the fact that there are tons of other environments not only nodejs
i only vote for resolve hooks and plugins because of all the diffrent environments as also package managers.
Node 14.8 added top-level await, but Node 12 doesn't have it. I think this omission is enough of a wart that starting at Node 12 is the wrong move.
What about removing Node12 and starts from Node 14?
One note here from https://github.com/microsoft/TypeScript/issues/46550#issuecomment-954348769—for users who have declaration emit enabled, error messages like this are probably going to be a common symptom of a dependency that needs to update their export map to include "types"
conditions:
error TS2742: The inferred type of 'T' cannot be named without a reference to '../../../node_modules/async-call-rpc/out/full'. This is likely not portable. A type annotation is necessary.
It feels pretty non-obvious that that’s what’s going on so I wanted to make a note of it here. Basically, the declaration emitter wants to print a type like import("async-call-rpc/out/full").T
, but it can’t because the package has an export map that doesn’t specify any "types"
. So the only way it can reach that module is with a relative import through node_modules, which is not allowed to be synthesized by declaration emit. If you look at the package’s package.json, you can see that the actual specifier that should be generated will probably be "async-call-rpc/full"
, but each of those entrypoints in the export map needs a "types"
if it’s going to be resolvable by TypeScript.
☝️ Actually, I was wrong about this particular example. This may be a common symptom for some packages if their typings are stored in a separate folder from the JS that the export maps point to, like RxJs does. However, if the .d.ts
files are colocated with the .js
/.mjs
/.cjs
files pointed to by the export map, this should “just work.” This is the case for async-call-rpc
, so the fact that the declaration emitter is complaining here is probably indicative of a module specifier resolution bug. (cc @Jack-Works)
Oh. Yes, I found that package actually exports the correct typing and JS file at /full
, not /out/full
.
There are some rough edges with the new resolution mode. I've been trying out this mode for two weeks.
The main issues I've hit are:
types
declaration (@types/koa-route
, raw-body
)export
field but no types
condition while the type declarations are in their own directory (rxjs
)Despite all of these issues, I still hope that the new resolution algorithm becomes available on stable TypeScript: the core seems good and bugs/UX can be iterated. The new resolution mode lets me get rid of tons hacks and makes it finally ergonomic to use ESM natively.
exports
with subpath mapping to expose my outputDir directly for deep-imports; while being fully compatible with npm|yarn link
and project references, and without having to abuse typeVersions
.wasm-bindgen
), I am now able to use import maps and remove dummy files used just so TypeScript does not complain about missing files.import.meta
or __dirname
.I was able to use node12
with Node, webpack, mocha, wasm-bindgen, native node modules and Angular 13.
In my personal experience, all of these improvements strongly outweight the current issues: it makes things so much simpler as it realigns TS behavior's with Node's. I hope that the current issues get fixed soon and I hope that the concerns will not delay the new resolution too much.
unstable import resolution issues in IntelliJ IDEA (looks to be related to #46389)
@demurgos did you get the right issue number here? This doesn't sound like it would be related to what you're talking about 🤔
I wanted to link the issue 46396
: "nodenext alternates between finding and not finding the imported package"
I've double checked my message: I linked it as #46396
but it looks like GitHub failed to resolve it properly. Editing my message to add a single space to force a refresh fixed the rendering.
What if TypeScript 5 assumes "type": "module"
by default?
It would be regrettable if we have to add "type": "module"
to our package.json in the far future, when many have migrated.
What if TypeScript 5 assumes
"type": "module"
by default?It would be regrettable if we have to add
"type": "module"
to our package.json in the far future, when many have migrated.
Just to be clear it is Node (not TypeScript) that requires "type": "module"
in package.json
and that isn't likely to change probably ever. TypeScript just reads the value to understand how Node will run the module, TypeScript doesn't control how the module is run.
at present i think the type fild in the package.json is less relevant as that is only a switch for the .js extension inside NodeJs Typescript at present 4.5+ detects the module type via import
and export
statments inside the .js files this should not change.
Typescript is not a NodeJS only Product at last i guess that.
ps i still have Javascript Projects without a package.json at all and i use the global installed typescript to typecheck them it works great and it should stay working.
For folks who are interested in testing esm-node out:
npm add typescript@4.5.0-dev.20211101--save-dev
yarn add typescript@4.5.0-dev.20211101 --dev
pnpm add typescript@4.5.0-dev.20211101 --dev
This is the closest nightly npm release to the RC, so can act as "4.5 but with ESM enabled" for your projects.
Are the node12
and nodenext
module options both being dropped for 4.5? Or just node12
?
edit: based on typescript@4.5.1-rc
the answer is "both"
tsconfig.json:27:15 - error TS4124: Compiler option 'module' of value 'nodenext' is unstable. Use nightly TypeScript to silence this error. Try updating with 'npm install -D typescript@next'.
27 "module": "nodenext", /* Specify what module code is generated. */
~~~~~~~~~~
Correct, neither will be available for TS 4.5.
For what it's worth, I can explain ts-node's short term plans around file extensions in import specifiers.
Today we already allow .js
extensions in imports when using our node ESM loader hooks. You can import ./foo.js
to execute ./foo.ts
. Our ESM resolver hook understands to resolve .js
to .ts
when the corresponding TS file is found on disk.
In the near-term, I'm working on adding resolver behaviors to CommonJS so that .js
extensions will also work for the CommonJS / require()
resolver. Once these changes are complete, users will be able to specify the .js
extension in all imports, whether they are using the ESM loader hooks, the CommonJS hooks, or both. This should hopefully eliminate a big source of confusion.
I have a pinned issue for this but hesitate to link it here, both to avoid confusing readers and to avoid too many distracting questions. It's a broad issue describing multiple resolver behaviors, file extensions being just one part of it.
- ts-node, Webpack, and Vite don't like imports with
.js
extensions, but TypeScript expects them. Not all of these can be configured with a plugin.
FYI Vite 2.7.0-beta.3 has added support for imports with .js
extensions when the importer is a ts module.
Will it be possible to use native dynamic import(…)
https://github.com/microsoft/TypeScript/issues/43329 in TypeScript 4.5 without module: nodenext
?
@ExE-Boss the related code is only in the node12 and nodenext features it is not isolated at present but i agree that this could be a extra PR and a extra tsconfig.json setting like preserveDynamicImport. As i see situations where you would want to transpil it and also situations where perserving them is the right choice.
I am mostly excited about support for the "exports" field since it is a key for dual packaging. I hope the "type: module" support to be a secondary concern for now, or at least not something that could slow down "exports".
This was the first Typescript beta I installed because I really wanted "exports". I had some issues, like "exports" not being effective unless a matching "typesVersions" was present. Things could work, but problems were hard to diagnose.
@ExE-Boss it looks like dynamic import isn’t transformed away in module: es2015
and up: playground
FYI Vite 2.7.0-beta.3 has added support for imports with
.js
extensions when the importer is a ts module.
@sodatea thanks for bringing this to our attention! Do you happen to have a link to an issue/PR on their side where this was discussed? I’d be curious to read about it.
FWIW, as a maintainer of a small corner of the ecosystem, I strongly second this idea:
I would like to see a world where we say "under this mode,
.ts
files must be covered by a"type": "module""
..cts
can do their own CommonJS thing, but they need to be in a.cts
file.
While I'm also sympathetic to @weswigham's comments on the design discussion thread, where folks want TS to help bridge the gap, it's not clear to me that you're going to be able to do that in a particularly clean way—and I think it's probably going to be net easier pedagogically and mechanically to tell people something along these lines:
Okay, here's the upgrade flow
- Set the new value in
tsconfig.json
.- Rename all your existing
.ts
files to.cts
.- Make sure all your tests pass!
- Convert all
.cts
files to.ts
, with ES module semantics, at will.
That basically matches how I would advise someone to migrate a JS library. It makes ES modules the new default; it preserves all existing behavior; and it also makes the migration status obvious and easy to reason about—and, importantly, finishing the migration is effectively zero-risk: someday you just don't have any CJS files left.
The bigger challenge here is the publication of modules in both modes (supporting exports.require
and exports.import
, right? I.e. needing separate configs for each mode (presumably extending a base config), with the resulting different semantics, if you choose to publish and support that way. 🤔
@ExE-Boss it looks like dynamic import isn’t transformed away in module: es2015 and up: playground
@andrewbranch Shouldn't this be documented as a breaking change then?
@sodatea thanks for bringing this to our attention! Do you happen to have a link to an issue/PR on their side where this was discussed? I’d be curious to read about it.
@andrewbranch Yeah, the issue was originally discussed at https://github.com/vitejs/vite/issues/3040 and the fix is https://github.com/vitejs/vite/pull/5510
@ExE-Boss it looks like dynamic import isn’t transformed away in module: es2015 and up: playground
@andrewbranch Shouldn't this be documented as a breaking change then?
No, because that’s the existing behaviour.
What I (and https://github.com/microsoft/TypeScript/issues/43329) want, is to support preserving dynamic import(…)
in module: commonjs
(and other non‑ES* module types).
@sodatea thanks for the info and thanks for driving that change in Vite! That’s a big step toward reducing friction for TS users who want to use proper ESM anywhere besides Node ❤️. It looks like that feature went through without much trouble, but feel free to loop me into TS-related issues in the future if you ever need someone from the team to weigh in or take a look at something!
What I (and #43329) want, is to support preserving dynamic import(…) in module: commonjs
Agreed, since CommonJS supports await import()
, then TS with module: commonjs
should too 👍
Its not just a nice-to-have, its critical for compatibility as seen here: https://github.com/microsoft/TypeScript/issues/43329#issuecomment-864187283
Perhaps that would need to happen in TS 5.x since it would be a breaking change, or maybe somehow enable with target: esnext
.
See Playground for more.
The node12
and nodenext
module emit modes include that.
@weswigham Yep you're right!
@orta Perhaps this is a bug in the playground after toggling "module" a few times.
See this video which can be reproduced with this link.
Correct, neither will be available for TS 4.5.
FYI the TS 4.5 release notes make no mention of that, maybe it would be helpful to surface that information? I made an attempt to migrate to ESM at work that failed for unrelated reasons, but I did it under the assumption that Node.js /\ ESM support was going to be available in TS 4.5 stable.
Thank you for your hard work!
Thanks for the heads up - I've opened https://github.com/microsoft/TypeScript-Website/pull/2149.
What version of TypeScript will support Node.js then, since this support got scrubbed from the v4.5 release? I can't find it tracked in any of the upcoming iteration plan issues or issue milestones.
@jaydenseric we're committed to shipping this as soon as it's ready; ideally 4.6, but the possibility of a further slip is always present
@skimmilk6877 let me give you some insights as Javascript got created there was "No Module System" so People Invented them. The Current JavaScript/ECMAScript standard has well defined Module System and that is the only source of truth for today. Everything else only shims/polyfills it or transpils to it.
The Browser Implementation is the only one that matters! having a clear url and protocol without additional resolve step is the only way to go so for local files it is file:// and i would also suggest to only accept unix style urls with that protocol so \ gets /
implementations (engines) can then use the protocol to add additional resolve implementations. for file and http urls there should be no additional resolve step at all in the src code or production code.
It's worth understanding that the implementations by Deno and Node are both compliant with the spec. So are implementations in browsers which resolve things somewhat differently than either! The spec intentionally allows a string identifier to be resolved in different ways by different implementations. This is a necessary design constraint given the many different environments that JavaScript modules exist in, and it's the only reason it's even possible for Node to add support at all! It's also what has allowed tools as different in implementation details as Ember CLI and webpack and TypeScript to use Node's module system under the hood while allowing developers to author in ES modules for the last 5–6 years even though Node itself didn't yet have built-in support for ES modules.
Happy American Thanksgiving, everyone! 🦃🥧🇺🇸
@chriskrycho sure they are both compliant as both are using v8 which is compliant and both transpil to it and there is no resolve in v8
There are two important ways in which your statements (including your clarification) are misguided at minimum:
To take a historical example, Ember’s use of ES Modules[^redesign] was quite different from either the browser/Deno model or the Node module. (There were good reasons for that, given when Ember adopted ES modules!) The Ember modules happened to be compiled to a bunch of AMD modules to run in the browser—not least because the browsers didn’t yet support the ES module spec, but also because the best authoring format isn’t necessarily the same as the most-optimized runtime format.
So, to reiterate, running in a browser or using transpilation is irrelevant. The Node implementation, including all of its resolution rules, is in and of itself spec compliant. So was Ember’s classic modules design. So are browsers’ and Deno’s use of URLs.
I understand (even if I don’t agree with) the idea that all resolution should just happen via URL. It definitely has its attractions. However, that’s simply not what compliance with the spec requires—and that’s actually a very good thing, given how diverse the JS ecosystem is. And, most importantly for this thread: whatever your or my personal tastes or preferences, TypeScript has to support the ecosystems which actually exist.
[^redesign]: currently in the midst of a long transition to use Node module resolution
@chriskrycho good point finaly i found a reason for a new (the next) Programming Language only to drop the existing Ecosystem in favor of a clean one that works. Thanks for your time and inspiration you finally got me on track again the JS/ES thing is dead we need something total new. Something that is a SuperSet of ECMAScript but it should be target environment aware.
GitHubDesktopSetup-x64.msi
It's been about two months now since the last message. Do you know what are the chances we'll get support for Node's ESM in the next stable release?
what is it with developers and wanting plugins in literally everything?
Open-closed principle.
"open for extension, but closed for modification"
@weswigham you sayed that you want to use exports: { "types": } in package.json but when i add that in current vscode insiders then it yields "types" is not allowed but i am not sure where that comes from. it is not from typescript or is it?
i guess it is not allowed to use something like "types" in exports only pathes are allowed. or path patterns "./types" would be valid
@DanielRosenwasser not sure if this has been reported, I could not find any issues explicitly mentioning it. But it seems the new "module": "nodenext"
resolution is ignoring typesVersions
, it is not mentioned in the documentation so curious if this was a deliberate decision.
I came across the issue when using the rxjs types, they rely on typesVersions
and types
to point to the correct files, and also use exports
. Playground Link
I am trying to figure out if I should put a PR in to rxjs to add the types
key to the exports
map or whether this is a bug with TypeScript's new ESM resolution?
Edit: It seems RXJS is mentioned a few times in this thread. Although that seems to be referencing the fact that it does not add
types
toexports
rather than the reliance ontypesVersions
.
typesVersions
is to main
what versioned types
conditions are to exports
.
@AndrewLeedham One of the things we plan to do before shipping this as stable is auditing the top N most popular npm packages that ship their own types and use package.json exports
. We’re aware that RxJS needs an update. If you or someone else makes that PR before we get to it, please ping me on it. Thanks!
For TypeScript 4.5, we've added a new module mode called
node12
to better-support running ECMAScript modules in Node.js. Conceptually, the feature is simple - for whatever Node.js does, either match it or overlay something TypeScript-specific that mirrors thes same functionality. This is what TypeScript did for our initial Node.js support (i.e.--moduleResolution node
and--module commonjs
); however, the feature is much more expansive, and over the past few weeks, a few of us have grown a bit concerned about the complexity.I recently put together a list of user scenarios and possibly useful scripts for a few people on the team to run through, and we found a few sources of concerns.
Bugs
Most complex software ships with a few bugs. Obviously, we want to avoid them, but the more complex a feature is, the harder it is to cover all the use-cases. As we get closer to our RC date, do we feel confident that what we're shipping has as few blocking bugs as possible?
I would like to say we're close, but the truth is I have no idea. It feels like we'll have to keep trying the features for a bit until we don't run into anything - but we have less than 3 weeks before the RC ships.
Here's a few surprising bugs that need to get fixed before I would feel comfortable shipping
node12
in stable.resolveJsonModule
can't be used withnode12
: https://github.com/microsoft/TypeScript/issues/46362.ts
and.tsx
files innode12
(unfiled, reported by @andrewbranch)package.json
changes in packages not tracked (unfiled, reported by @DanielRosenwasser)UX Concerns
In addition to bugs we found, there are just several UX concerns. Package authoring is already a source of confusion in the TypeScript ecosystem. It's too easy to accidentally shoot yourself in the foot as a package author, and it's too hard to correctly consume misconfigured packages. The
node12
mode makes this a whole lot worse. Two filed examples of user confusion:export
field is confusing to configure and diagnose: https://github.com/microsoft/TypeScript/issues/46334While there might be a lot of "working as intended" behavior here, the question is not about whether it works, but how it works - how do we tell users when something went wrong. I think the current implementation leaves a lot of room for some polish.
But there are some questions about this behavior, and we've had several questions about whether we can simplify it. One motivating question I have is:
When a user creates a new TypeScript project with this mode, when would they not want
"type": "module"
? Why? Should that be required by default?We've discussed this a bit, and it seems a bit strange that because we want to cover the "mixed mode" case so much, every Node 12+ user will have to avoid this foot-gun.
I would like to see a world where we say "under this mode,
.ts
files must be covered by a"type": "module"
"..cts
can do their own CommonJS thing, but they need to be in a.cts
file.Another motivating question is:
Why would I use
node12
today instead ofnodenext
?Node 14.8 added top-level
await
, but Node 12 doesn't have it. I think this omission is enough of a wart that starting at Node 12 is the wrong move.Ecosystem
The ecosystem is CONFUSING here. Here's a taste of what we've found:
.js
extensions, but TypeScript expects them. Not all of these can be configured with a plugin..ts
extensions, but TypeScript doesn't allow them!export
fields, but don't have atypes
sub-field withinexport
(e.g. RxJS, Vue 3).export
fields, but their@types
package might not reflect that.The last two can be easily fixed over time, though it would be nice to have the team pitch in and help prepare a bit here, especially because it's something that affects our tooling for JavaScript users as well (see https://github.com/microsoft/TypeScript/issues/46339)
However, the first two are real issues with no obvious solutions that fall within our scope.
There's also other considerations like "what about import maps?" Does TypeScript ever see itself leveraging those in some way, and will package managers ever support generating them?
Guidance
With
--moduleResolution node
, it became clear over time that everyone should use this mode. It made sense for Node.js apps/scripts, and it made sense for front-end apps that were going to go through a bundler. Even apps that didn't actually load fromnode_modules
could take advantage of@types
in a fairly straightforward way.Now we have an ecosystem mismatch between Node.js and bundlers. No bundler is compatible with this new TypeScript mode (and keep in mind, back-end code also occasionally uses a bundler).
Here's some questions I wouldn't know how to answer confidently:
node12
ornodenext
?"type": "module"
- should we always recommend.mts
?Next Steps
I see us having 3 options on the table: