1

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';
2
  • How does the import look like in the TypeScript file? Commented Jun 1, 2021 at 17:17
  • on typescript it's import { ENV, redisConfig } from './config'; but the file is named "config.ts" Commented Jun 1, 2021 at 21:33

1 Answer 1

1
+50

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.