19

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

curtc
  • 263
  • 1
  • 3
  • 7
  • 2
    Do you have a `package.json` file at the root of your workspace? – Matt Bierner Mar 20 '20 at 20:36
  • No. Do I need one? Again, lots of JS stuff DOES autocomplete. – curtc Mar 21 '20 at 00:37
  • `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 @MattBierner – abondoa Mar 21 '20 at 22:46
  • 1
    Thanks @abondoa 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"? – curtc Mar 22 '20 at 15:59
  • @curtc you'll need to run `npm install --save-dev @types/node` as well. See my newly added answer below. – abondoa Mar 23 '20 at 21:39

3 Answers3

38

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.

abondoa
  • 1,613
  • 13
  • 23
  • vscode website doesn't mention this? are you sure this isn't fixing some inherent problem and just side stepping it? – Muhammad Umer Apr 16 '21 at 14:54
  • 1
    @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 (https://github.com/Microsoft/TypeScript/issues/13196). By default `typeRoots` is operating on `node_modules/@types` (https://www.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. – abondoa Apr 17 '21 at 21:11
  • 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? – Nathan Goings Sep 13 '21 at 17:03
  • @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. – abondoa Sep 14 '21 at 19:05
  • @abondoa, it was due to me not installing NPM. I made an answer below with my solution. – Nathan Goings Sep 14 '21 at 19:59
2

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.

Nathan Goings
  • 1,145
  • 1
  • 15
  • 33
-1

If you are using pure javascript for your node app, when including the required modules, they should be defined with single quotes instead of double-quotes. If you were using a code formatter extension like "Prettier" for instance, it adds it by default before the IntelliSense, then you would have to update your settings to use single quote.

andrefg
  • 1
  • 1