Valerio
Valerio

Reputation: 2590

Configure typescript to output imports with extensions, ex: `from './file.js``

Using node 14.x I would like to switch my project to full ES Modules, as it's now supported.
So I enabled on package.json "type": "module"

and my tsconfig.json looks like that:

{
  "compilerOptions": {
    "outDir": "dist",                         /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
    "allowSyntheticDefaultImports": true,
    "lib": ["ES2020"],
    "module": "ESNext",
    "moduleResolution": "node",
    "target": "ES2020"
  }
}

But the output files have unspecified extension in imports, it's instead REQUIRED for node 14.x to specify full file ext

For example:

import { ENV, redisConfig } from './config';

should instead be:

import { ENV, redisConfig } from './config.js';

Upvotes: 1

Views: 175

Answers (1)

EcksDy
EcksDy

Reputation: 1654

TS won't handle that for you, but you can run Node with the node --experimental-specifier-resolution=node parameter. Source

Unfortunately it seems that TS isn't going to support adding extension to the end of the import, since apparently it's not as simple as adding .js to the end of the import.

Upvotes: 1

Related Questions