Manfred Steiner
Manfred Steiner

Reputation: 1295

How to use global Node packages with VS Code

If Node.js packages are globally installed, they can be used by Node.js applications. But Visual Studio Code will not accept these packages and indicate an error, as long as these packages are not installed locally in the project directory (inside the subdirectory node_modules).

For example:

import * as net from 'net';

... 'net' is marked with a red underline as long as you do not install npm install --save @types/node. If you install that package globally via npm -g install @types/node, the package would be available, but code (V 1.17.1) will not recognize it.

So my question, is there a way to configure code to recognize global installed Node.js packages?

Upvotes: 3

Views: 7429

Answers (2)

toolcreator
toolcreator

Reputation: 1

This is rather a workaround/partial answer (and I cannot comment yet):

As long as all your scripts that you don't want to create a full package.json for (e.g., simple standalone scripts) have a common root directory in the filesystem, vscode seems to recognize a "global" package.json and node_modules directory at that root.

More concretely, you could run npm i -D @types/node in your home directory, and vscode finds the types for all scripts somewhere below the home directory (i.e., $HOME/**/*.ts).

(Only tested on Linux with a single file, and vscode 1.64.1.)

Upvotes: 0

Matt Bierner
Matt Bierner

Reputation: 65175

IntelliSense for global modules is not supported as of VS Code 1.18. Please file an new issue against TypeScript if you really need this feature

However you really should not need to install @types packages when working with JavaScript in VS Code. Automatic types acquisition should kick in and download these for you. There are also explicitly force these types to be downloaded by adding the following to your jsconfig.json

"typeAcquisition": {
    "include": [
        "node"
    ]
}

For TypeScript projects, you should install @types locally as dev dependencies.

Upvotes: 4

Related Questions