Typescript: Add js extension to import/export

Created on 5 Oct 2017  路  5Comments  路  Source: microsoft/TypeScript

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?

Duplicate

Most helpful comment

I'm having this issue in two directions:

  • I started the development without any extension.
  • I could write test and run them with jest perfectly.
  • When I tried to compile the modules and load them as ES6 modules in Chrome it didn't work because they had no extension.
  • I added .js extension manually to all imports.
  • Now it compiles and runs in Chrome but jest complains:

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?

All 5 comments

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:

  • I started the development without any extension.
  • I could write test and run them with jest perfectly.
  • When I tried to compile the modules and load them as ES6 modules in Chrome it didn't work because they had no extension.
  • I added .js extension manually to all imports.
  • Now it compiles and runs in Chrome but jest complains:

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielRosenwasser picture DanielRosenwasser  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

wmaurer picture wmaurer  路  3Comments

manekinekko picture manekinekko  路  3Comments