Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Node.js code autocompletion in VSCode?

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'! :(

like image 971
curtc Avatar asked Mar 20 '20 15:03

curtc


2 Answers

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.

like image 191
abondoa Avatar answered Oct 06 '22 10:10

abondoa


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.

like image 29
Nathan Goings Avatar answered Oct 06 '22 10:10

Nathan Goings