48

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?

RBT
  • 24,161
  • 21
  • 159
  • 240
Lev
  • 13,856
  • 14
  • 52
  • 84
  • I don't believe so, but there's a github issue for this that you can track - https://github.com/Microsoft/vscode/issues/19439 – heylookltsme Apr 21 '17 at 12:06

4 Answers4

48

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
Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
  • 1
    Requires using TypeScript 2.9 or newer in the workspace. What if I'm using Javascript? – Rafa0809 Jun 07 '19 at 20:38
  • Same. JavaScript powers VS Code's TypeScript support but if you are just a JS user you are unlikely to have changed the default workspace version – Matt Bierner Jun 07 '19 at 22:01
  • 3
    Is there any way to also make it save the changed files automatically? I find it really annoying that I have to go dig up all the files where the import was changed to save them. – Rikke Bendlin Gammelmark Jun 21 '19 at 07:59
  • 1
    have a look at https://stackoverflow.com/questions/37014171/how-can-we-save-all-files-in-vscode-like-we-do-in-visual-studio, which help setting up save all files automatically. In my machine, by default, it is Ctrl + K S, but you can change the shortcut to anything you like. – CHANist Sep 05 '19 at 01:42
  • @MattBierner Where should I add "javascript.updateImportsOnFileMove.enabled" in vs code? – Stack Aug 13 '20 at 18:37
  • Try adding it in the [VS Code settings](https://code.visualstudio.com/docs/getstarted/settings) – Matt Bierner Aug 13 '20 at 21:31
  • 14
    i'm not sure when, but this seems to have stopped working for me (or perhaps i'm misremembering and it only worked for TS not JS). require() statements are no longer being updated on moves/renames even though i have the javascript.updateImportsOnFileMove.enabled set to always. Not running a lot of extensions - only the Angular essentials "pack" – jamey graham Sep 11 '20 at 12:49
15

First go settings

enter image description here

Search 'update import'

enter image description here

You can select:

  • prompt (prompt on each rename)
  • always (Always update path automatically)
  • never (Never rename paths and don't prompt)
Wael Zoaiter
  • 686
  • 1
  • 7
  • 21
Dhamith Kumara
  • 499
  • 5
  • 9
12

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.

Gus
  • 6,545
  • 4
  • 36
  • 39
5

Import updating stopped working for me after I set up typescript in my project.

The solution was to follow: https://github.com/microsoft/vscode/issues/66937#issuecomment-475087628

Specifically, to add a jsconfig.json file in the root of my project containing:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2016",
    "jsx": "preserve"
  },
  "exclude": ["node_modules", "**/node_modules/*"]
}
owencm
  • 8,384
  • 6
  • 38
  • 54