node.js - 在 VS Code 中启用 Javascript 的隐式类型检查会导致 "Cannot find name ' require'"错误

标签 node.js visual-studio-code

我制作了一个非常简单的 Node 项目(npm init,接受所有默认值)并在 VS Code 中打开它。我关注了the instructions in the docs要打开全局隐式类型检查:在用户首选项中将 javascript.implicitProjectConfig.checkJs 设置为 true

现在,当我尝试在代码中使用 require() 时,我收到一条红色波浪下划线,并显示错误 [js] Cannot find name 'require'.我是否需要做一些事情来告诉 Typescript 服务器这将在 Node 环境中运行,因此 require 将作为全局提供?

预计到达时间:我并不完全清楚生成“找不到名称”错误的内容与创建智能感知建议的内容之间的脱节。尽管存在 require() 错误,Intellisense 仍然可以正常工作。如果我npm install --save moment,然后写

const moment = require("moment");
moment.

然后我得到了 Moment 函数的 Intellisense 补全,尽管 require 下面有一条红色的波浪线。

最佳答案

您可能必须通过以下方式安装 Node 类型 npm install --save-dev @types/node 这将在本地安装 Node 类型。

关于node.js - 在 VS Code 中启用 Javascript 的隐式类型检查会导致 "Cannot find name ' require'"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45347599/

相关文章:

javascript - NodeJs如何将变量传递给导出函数

node.js - Loopback 4 Debugger nodemon 解决方案

typescript - 无法重新声明 block 作用域变量 'jQuery'

code-formatting - 如何在 Visual Studio Code (VSCode) 中设置代码格式?

node.js - winston.debug 在终端上不显示任何内容

node.js - 将 Node.js API 连接到 Microsoft Azure/Azure CLI 上的虚拟机

java - VS Code生成maven项目失败的解决办法

visual-studio-code - 是什么 !在 vscode 中我的文件名旁边签名

visual-studio-code - Intellisense 无法处理多个文件

Node.js 将值从一个 api 传递到另一个 api