60

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?

1

4 Answers 4

56

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
Sign up to request clarification or add additional context in comments.

7 Comments

Requires using TypeScript 2.9 or newer in the workspace. What if I'm using Javascript?
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
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.
have a look at stackoverflow.com/questions/37014171/…, 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.
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"
|
24

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)

2 Comments

Hmm, looks like this setting was removed?
no still available
16

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.

5 Comments

strange, your soln doesn't seem to work for an instantiated repo with TS 4.1.2 and VSCode 1.52.1, but when I create a new repo, it works OK...
Fixed it for me by removing "include":[], from my autogened tsconfig
@joshp Thanks for this comment. Remove "include" fixed it for me as well
This also fixed it for me. I just left the include out, and I can't see a difference. Doing so caught a bug, too. Somewhere I was referring to an import by src/File.ts
It didn't work for me (vscode 1.79.2)
6

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/*"]
}

1 Comment

It didn't work for me (vscode 1.79.2)

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.