DEV Community

Shane Mitchell
Shane Mitchell

Posted on • Originally published at shanemitchell.dev on

How to: Enable JavaScript auto-import suggestions in VS Code

Auto import suggestions

Auto-import problems

If you work on JavaScript projects in VS Code, you’ve almost certainly been frustrated with the hit-or-miss nature of the auto-import suggestions. When you try to import code from elsewhere in your project, the correct suggestion is nowhere to be found.

Import missing from suggestions

There are extensions to help with this, but it turns out VS Code now handles this natively – using the jsconfig.json file – and luckily it’s very simple to setup.

Create the jsconfig.json file

As of 2018, VS Code treats all JS files as separate from one another. In order to tell it that your files are all part of an Explicit Project , you need to create and configure a jsconfig.json file (this is a descendant of tsconfig.json, which is required for TypeScript projects).

Here’s how:

  1. Create the jsconfig.json file at the root of your project.
  2. (Optional) Configure the compiler options.

Technically an empty file will suffice to tell VS Code this is a project, but you’ll likely want to configure a few things.

There’s a lot of options that can be set, but a few that I like to set for React projects are:

// jsconfig.json at project root

{
  "compilerOptions": {
    "module": "ES6",
    "jsx": "preserve",
    "checkJs": true
  },
  "exclude": ["node_modules", "**/node_modules/*"]
}
Enter fullscreen mode Exit fullscreen mode

See the docs for details and more config settings.

And it works! 👍

Now that VS Code knows to treat all your JS files as an explicit project, auto-import suggestions will populate with all your relevant exported code.

Auto import suggestion working

Hope that helps! For more VS Code import tips, see my post on automatic import sorting.

Top comments (12)

Collapse
 
emarubi profile image
emarubi

I'm a software engineer too and I use TypeScript, React.js, and Node.js daily.
Thanks for this article who helped me to solve this automatic import in Vs Code issue.

May I ask you which font and theme you use in Vs Code?
I saw it many times in tutos and I would like to have it :)

Have a nice day!
Emanuela

Collapse
 
ostgals profile image
Andrejs Kuzmins

Cobalt2 by Wes Bos

Collapse
 
shanesc profile image
Shane Mitchell

Glad it helped!

Like Andrejs said, the theme is Cobalt2 and the font is Operator Mono. Fira Mono is another similar, free mono spaced font :)

Collapse
 
jaboulos profile image
ghostagent

Nice, helpful and to the point

Collapse
 
zonaguillermo profile image
Zona Guillermo

Hi Shane,

After reading your post I have a doubt. In the last line of your script:

"exclude": ["node_modules", "**/node_modules/*"]
Enter fullscreen mode Exit fullscreen mode

Shouldn't it be "node_modules/**/*" instead of "**/node_modules/*" ?

Collapse
 
shanesc profile image
Shane Mitchell

Hi @zonaguillermo, thanks for reading :)

This is the suggested pattern from the VS Code docs.

It is supposed to exclude any node_modules folders at any depth, not just the root level node_modules folder (see this stack overflow post.

TBH I'm not entirely sure when this case would come up, but it certainly won't hurt to include it, since you wouldn't want Intellisense to be searching any node_modules folders.

It's likely that "exclude": ["node_modules"] is sufficient though.

Collapse
 
rogue_paradigms profile image
Rogue Paradigms

For anyone reading in the future, "**/node_modules/*" helps when you have sub-directories/sub-projects that have their own node_modules [and package.json]

Collapse
 
zonaguillermo profile image
Zona Guillermo

Thanks Shane for de response,

I read these post and I see it more clearly now

Collapse
 
sergo profile image
Sergo • Edited

I'm using TailwindCSS, so after creating jsconfig.json I had an issue:

Cannot find module 'tailwindcss'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?'

If I delete "module": "ES6", the error will disappear.

Collapse
 
sergo profile image
Sergo

I don't know why, but if you add the string into jsconfig.json:

"include": ["src//*.js", "src//*.jsx"],

it will work.
This can be helpful -> github.com/romansndlr/react-vite-r...

Collapse
 
rita_hlinska_607c96554237 profile image
Rita Hlinska • Edited

Doesn't work. I am using Vue.js
Image description

Image description

Collapse
 
michele_buzzoni_4ab920134 profile image
Michele Buzzoni • Edited

if i add checkJs then i have also error on types, like i'm working with ts files