如何在 VSCode 中启用 Node.js 代码自动完成功能? [英] How to enable Node.js code autocompletion in VSCode?

查看:27
本文介绍了如何在 VSCode 中启用 Node.js 代码自动完成功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 Visual Studio Code 和 Node.js,它们基本上都可以工作,但自动完成功能(完全)不工作.如果我输入控制台".我确实看到了一个列表弹出窗口.同样,如果我这样做:const http = require("http");http.

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.

但如果我只是输入过程".我什么也没看到.事实上,只要我输入'.'代码自动完成过程"到处理指令".我期待看到 argv 弹出,以及如果您在 Node 提示符下键入process",您会看到的所有其他内容.

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.': Yay -- it works!

但这是我在输入过程"时看到的内容.(我必须将自动完成的ProcessingInstruction"改回process"):嘘——它不知道过程"!:(

But here's what I see when I type 'process.' (I have to change the autocompleted 'ProcessingInstruction' back to 'process'): Boo -- it doesn't know 'process'! :(

推荐答案

您需要将 Node JS 中的类型告诉 VS Code(正如您在评论中自己指出的那样).为此,您可以为运行以下命令的节点安装类型(假设您已经运行了 npm init):

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

它将安装 Node JS 的类型,VS Code 会自动选择这些类型,并且您将自动完成所有 Node JS 特定的事情.您甚至不必重新启动 VS Code.

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.

当您向项目添加更多依赖项时(如果您愿意的话).他们中的许多人也有一个 @types/X 包(如果他们没有已经包含在包中的),这也将允许自动完成.

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.

这篇关于如何在 VSCode 中启用 Node.js 代码自动完成功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆