@types/node
. When I write import http = require('http')
or import * as http from 'http'
, the type of the object I'm importing is inferred: const fs = require('fs');
, the type is any
. Why is that? Can I do anything about it?TL;DR
A possible fix for this is to import the types for Node.js.
Full Solution
The solution for this issue is to import the types for Node.js.
This is available as an npm
package . It contains all of the types for Node.js.
To install it, you need to run the following command.
$ npm install --save-dev @types/node
The reason the --save-dev
flag is on the install command is because it isn't supposed to be a dependency, but instead a dev (development) dependency. This will not install this package if, for example, your project is installed as an npm
module.
After this, IntelliSense will automatically pick up the type definitions, and will add auto-correct your Node.js code, including doing some other handy things.
If the type definitions don't get picked up, then the best solution is to restart Visual Studio Code. If that doesn't work, make sure that this is in your package.json
file.
{
"devDependencies": {
"@types/node": "^17.0.31"
}
}
Note: The version was correct at the time of post.
If it isn't, then add that code in your package.json
file, save it, and run the following command.
$ npm install
This should install it successfully.
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.