You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeScript ES modules incompatible with browser and Node.js #33306
Error: Cannot find module '/home/paul/dev/pauldraper/typescript-nodejs/out/lib' imported from /home/paul/dev/pauldraper/typescript-nodejs/out/main.js
Playground Link:
Related Issues:
The issue is that while Node's CommonJS modules use a list of implicit file extensions, Node and browsers require the extension.
And neither Typescript's classic nor node resolution strategies permit a file extension. This choice allows tsc to not touch the import specifiers, as the .ts/.d.ts/.tsx/.js/.jsx/.json relationship stays the same before and after compilation.
Solutions:
Nothing. End-user maintains a post-process that either modifies the outputted import specifiers, or modifies the outputted file locations.
TypeScript adds a new --module value ESBrowser, which transforms the output import specifiers to have .js extension.
TypeScript adds a new option --outputExtension which when false or 'remove', strips the extension from the outputted file.
TS allows .js in module imports and that works correctly. Even when you're importing .ts or other file types that won't have .js extension until after compilation. You can't import .json.
Jack Works (@Jack-Works), sorry, but wouldn't this require more than just file extension? For example, according to the Node.js documentation, there is nothing special about index.js when using ES modules, so using Node module resolution might produce code that will fail in runtime. Or am I missing something?
Jack Works (@Jack-Works), sorry, but wouldn't this require more than just file extension? For example, according to the Node.js documentation, there is nothing special about index.js when using ES modules, so using Node module resolution might produce code that will fail in runtime. Or am I missing something?
Yeah my PR doesn't include solution for Node. It's for (relative files of) browser and deno.
We have previously discussed this in our design meetings and we are not likely to change this behavior or add an option to automatically add extensions to imports or strip out extensions.
You can also configure VS Code to add an implicit file extension for automatic imports using the typescript.preferences.importModuleSpecifierEnding setting:
Activity
0kku commentedon Sep 8, 2019
TS allows .js in module imports and that works correctly. Even when you're importing .ts or other file types that won't have .js extension until after compilation. You can't import .json.
pauldraper commentedon Sep 9, 2019
Oh interesting. Using
.jsimports does fix it.This appears to be undocumented?
The closest the documentation says is
Jack-Works commentedon Nov 17, 2019
#35148
n-romaniv commentedon Jan 12, 2020
Jack Works (@Jack-Works), sorry, but wouldn't this require more than just file extension? For example, according to the Node.js documentation, there is nothing special about index.js when using ES modules, so using Node module resolution might produce code that will fail in runtime. Or am I missing something?
Jack-Works commentedon Jan 12, 2020
Yeah my PR doesn't include solution for Node. It's for (relative files of) browser and deno.
4 remaining items
rbuckton commentedon Jul 8, 2021
TypeScript uses the literal import you wrote, and will attempt to resolve a
.jsto a.tsat compile time if one is available:We have previously discussed this in our design meetings and we are not likely to change this behavior or add an option to automatically add extensions to imports or strip out extensions.
You can also configure VS Code to add an implicit file extension for automatic imports using the
typescript.preferences.importModuleSpecifierEndingsetting: