Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can VS Code automatically update JavaScript and TypeScript import paths on file rename/move?

Tags:

Is there a module for vscode that would update paths towards files? e.g. if I have:

import './someDir/somelib' 

and I rename or move somelib, would it automatically update the file path in all the files where it is being referred as above?

like image 912
Lev Avatar asked Apr 21 '17 12:04

Lev


People also ask

How do I refresh Vscode?

To refresh the currently active viewChoose Refresh from the Window menu, or click the Refresh button in the toolbar.

How do I move files in Vscode?

Move is available by right-clicking the file in the sidebar after installing File Utils from the Visual Studio Code Marketplace. Show activity on this post.


2 Answers

This feature was added for JavaScript and TypeScript in VS Code 1.24 (tracking issue)

When you move or rename a file, you will now be prompted to see if you want to update imports:

enter image description here

This is controlled by the javascript.updateImportsOnFileMove.enabled and typescript.updateImportsOnFileMove.enabled settings. Valid values are:

  • "prompt" — The default. Prompt for each file rename/move
  • "always" — Always try to update imports automatically without prompting
  • "never" — Do not update imports and do not prompt
like image 155
Matt Bierner Avatar answered Oct 12 '22 18:10

Matt Bierner


As @jamey graham mentioned, it stopped working for me in a React typescript project. The fix was to update the tsconfig.json file to have:

{   "compilerOptions": {     "baseUrl": "./src",     // ...   }, } 

and to remove the "include": ["src"] option just for moving the files. VS Code should update the paths nicely after this.

You can bring back the "include": ["src"] option after this.

I do not know why removing the "include": ["src"] worked though.

If this doesn't do the trick, try opening a .ts file and trigger the Typescript: Restart TS server option in the command prompt and try again.

like image 45
Gus Avatar answered Oct 12 '22 18:10

Gus