How to enable Node.js code autocompletion in VSCode?
Asked Answered
D

2

23

I have installed Visual Studio Code and Node.js and both basically work, but autocomplete is not (completely) working. If I type 'console.' I do indeed see a list popup. Likewise if I do: const http = require("http"); http.

But if I simply type 'process.' I don't see anything. In fact as soon as I type '.' Code autocompletes 'process' to 'ProcessingInstruction'. I was expecting to see argv pop up, along with all the other stuff you see if you type 'process' at a Node prompt.

Here's what I see when I type 'console.': enter image description here Yay -- it works!

But here's what I see when I type 'process.' (I have to change the autocompleted 'ProcessingInstruction' back to 'process'): enter image description here Boo -- it doesn't know 'process'! :(

Dripping answered 20/3, 2020 at 15:54 Comment(5)
Do you have a package.json file at the root of your workspace?Amicable
No. Do I need one? Again, lots of JS stuff DOES autocomplete.Dripping
process is NodeJS-specific, so VS Code will need to know that you are working with Nodejs. Running npm init will likely do the trick. This will also create the package.json file mentioned by @MattBiernerCummings
Thanks @Cummings for the tip. I had indeed not run npm init. However I tried that, I now have a package.json, but I still don't get autocomplete for 'process', even after restarting Code. Does autocomplete for 'process' work for you? How does Code know "this is Node"?Dripping
@Dripping you'll need to run npm install --save-dev @types/node as well. See my newly added answer below.Cummings
C
52

You will need to tell VS Code about the types in Node JS (as you hit at yourself in the comment). To do this you can install the types for node running the following command (assuming you have already run npm init):

npm install --save-dev @types/node

It will install the types for Node JS, which VS Code automatically picks up and you'll be auto-completing all Node JS-specific things going forward. You don't even have to restart VS Code.

As you are adding more dependencies to your project (if you will be doing so). Many of them have a @types/X package as well (if they don't have the already included in the package), which will allow autocomplete as well.

Cummings answered 23/3, 2020 at 21:33 Comment(5)
vscode website doesn't mention this? are you sure this isn't fixing some inherent problem and just side stepping it?Ur
@MuhammadUmer To be honest, I have not been able to find the documentation on it either. My understanding is that VS code looks for the typeRoots in tsconfig.json for global types (github.com/Microsoft/TypeScript/issues/13196). By default typeRoots is operating on node_modules/@types (typescriptlang.org/tsconfig#typeRoots), which means that if you install any typescript types in that folder, they should be visible in VS code - at least that is the behavior that I see.Cummings
I have a folder with no configuration, just raw *.js files. Autocomplete works fine. I have another system with the same files and and no Autocomplete. On the broken system, NPM is not installed and the network is limited. Maybe VSCode is doing some behind-the-scenes configuration?Taiwan
@NathanGoings this might be a different thing altogether if you are not getting any JS autocomplete at all in VS Code. You can try posting a separate question with a minimal reproducible example.Cummings
@abondoa, it was due to me not installing NPM. I made an answer below with my solution.Taiwan
T
3

Per Microsoft's Documentation: https://code.visualstudio.com/docs/nodejs/working-with-javascript

IntelliSense for JavaScript libraries and frameworks is powered by TypeScript type declaration (typings) files.

Automatic type acquisition requires npmjs, the Node.js package manager, which is included with the Node.js runtime.

In my situation, I do not have npmjs installed and that's why automatic type acquisition fails.

*Edit, that is, after installing npm, my autocomplete starting working successfully for node related hints.

Taiwan answered 13/9, 2021 at 17:23 Comment(1)
To make it work, I had to restart VSCode after installing node.Nab

© 2022 - 2024 — McMap. All rights reserved.