简体   繁体   中英

How to get NodeJS and node-modules type hints in VS Code?

I have installed VS Code and want to create a simple Express app.

My code right now just looks like this:

import express from "express"

const HTTP_PORT = 1*process.env.PORT || 66600;

const app = express()


app.listen()

When I hover process.env I just get any , instead of {[name:string]:string} or some such. Even process itself is an any type. When I hover app , I just get Function , no hints for stuff like listen , get etc.

I installed these extensions:

  • JavaScript and TypeScript Nightly
  • Node.js Modules Intellisense

How do I configure my dev environment to get full type hints for packages and nodejs core libraries?

You can try to install a package containing the type definitions relative to node.js such as @types/node package .

To install it, just type

npm install --save @types/node

or

yarn add @types/node

...following your package system

Note that you could also install other @type packages following what package definitions are missing.

I think you migght need to add express types explicity, do npm i @types/express or yarn add @types/express . With regards to process.env, you cant get type inference because typescript doesnt know anything about your a files that are not in its scan area, its only looking for node modules, ts,and js files when your env variables are in your zshrc file or bashrc file or whatever it is on windows, maybe there is a vs-code plugin for it but im not aware of that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM