TypeScript Version: 2.5.2
Code
export { MyClass } from './a/b' // This is a file
In Chrome 61 Modules are supported out of the box. Node knows that this is a file, but when run in Chrome it thinks it is a directory and then it fails when it is loaded because it is trying to load index.js instead of b.js.
When changed to:
export { MyClass } from './a/b.js'
Chrome loads the file correctly. Is there a way to have the compiler add the .js extension on import/export without having to hard code it into all the files?
The compiler does not rewrite module names for you. a module name is a resource identifier, and will be emitted verbatim in most cases.
You should write the module name as you expect it to work at runtime; the compiler should also be able to understand that ./a/b.js points to ./a/b.ts at design time.
Please see similar discussions in https://github.com/Microsoft/TypeScript/issues/18951 and #16640
Such thing really should be done by loaders, until TS approach doesn't break any standards.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
I'm having this issue in two directions:
.js extension manually to all imports.Cannot find module './math.js' from 'vector.ts'
The only solution I see is for the compiler to add .js to import statements, maybe can this be under a flag?
I've seen the related issues but this is different since it's only related to the extension Typescript is changing. We're not talking here about paths, but if tsc is creating a .js file for each .ts file it makes sense to change/add the extension on the import statements from no extension or from .ts to .js, right?
Most helpful comment
I'm having this issue in two directions:
.jsextension manually to all imports.Cannot find module './math.js' from 'vector.ts'The only solution I see is for the compiler to add
.jsto import statements, maybe can this be under a flag?I've seen the related issues but this is different since it's only related to the extension Typescript is changing. We're not talking here about paths, but if
tscis creating a.jsfile for each.tsfile it makes sense to change/add the extension on the import statements from no extension or from.tsto.js, right?