user3330840
user3330840

Reputation: 7369

Force the auto import to use ".js" extension in TypeScript Visual Studio Code Editor

The auto-import completion feature sometimes adds the .js extension, but not all the time. However, without the extension in the TypeScript source, the tsc compiler won't add the extension to the emitted JavaScript file which can create run-time issues such as module not found error.

For details of the problem please refer to: Appending .js extension on relative import statements during Typescript compilation (ES6 modules).

The question is: is there any way to configure the automatic import completion feature to force to append the.js extension in the import statements? Or are there any add-ons or VS Code extensions that can achieve this?

This minor thing has been extremely annoying at times!

Upvotes: 8

Views: 3484

Answers (2)

felix0808
felix0808

Reputation: 81

Sublime Text version

Install package "LSP-typescript". Click Preferences -> Package Settings -> LSP -> Servers -> LSP-typescript. Add following json

{
  "initializationOptions": {
    "preferences": {
      "importModuleSpecifierEnding": "js"
    }
  }
}

Or use this in tsconfig

{
  "compilerOptions": {
    "moduleResolution": "nodenext", // <- Necessary to get imports with .js
  },
  "exclude": ["node_modules"]
}

Upvotes: 1

user3330840
user3330840

Reputation: 7369

I found that now the VSCode has it in the Preferences or Settings Ctrl + , under the entry: "typescript.preferences.importModuleSpecifierEnding": "js",.

Upvotes: 13

Related Questions