问题描述
我做了一个非常简单的 Node 项目(npm init
,接受所有默认值)并在 VS Code 中打开它.我按照 中的说明进行操作docs 打开全局隐式类型检查:在用户首选项中将 javascript.implicitProjectConfig.checkJs
设置为 true
.
I made a very simple Node project (npm init
, accept all defaults) and opened it in VS Code. I followed the instructions in the docs to turn on global implicit type checking: set javascript.implicitProjectConfig.checkJs
to true
in user prefs.
现在,当我尝试在代码中使用 require()
时,我得到一个带有错误 [js] 找不到名称 'require' 的红色波浪线下划线.
我是否需要做一些事情来告诉 Typescript 服务器这将在 Node 环境中运行,因此 require
将作为全局提供?
Now, when I try to use require()
in the code, I get a red squiggly underline with the error [js] Cannot find name 'require'.
Do I need to do something to tell the Typescript server that this will run in a Node environment, and thus require
will be provided as a global?
ETA:我不完全清楚产生无法找到名称"错误的任何东西与产生智能感知建议的任何东西之间的脱节.尽管出现 require()
错误,Intellisense 仍能正常工作.如果我npm install --save moment
,然后写
ETA: I'm not totally clear on the disconnect between whatever is generating the "Cannot find name" error, and whatever is creating Intellisense suggestions. In spite of the require()
error, Intellisense works correctly. If I npm install --save moment
, then write
const moment = require("moment");
moment.
然后我得到了 Moment 函数的 Intellisense 完成,即使 require
下面有一条红色波浪线.
then I get Intellisense completion of Moment functions, even though require
has a red squiggly line under it.
推荐答案
您可能必须通过以下方式安装节点输入npm install --save-dev @types/node
这将在本地安装节点类型.
You may have to install node typing vianpm install --save-dev @types/node
This will install node types locally.
这篇关于在 VS Code 中启用对 Javascript 的隐式类型检查会导致“找不到名称‘需要’";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!