Coderer Coderer - 2 years ago 103
Node.js Question

Enabling implicit type checking for Javascript in VS Code results in "Cannot find name 'require'" error

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
in user prefs.

Now, when I try to use
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
will be provided as a global?

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
error, Intellisense works correctly. If I
npm install --save moment
, then write

const moment = require("moment");

then I get Intellisense completion of Moment functions, even though
has a red squiggly line under it.

Answer Source

You may have to install node typing via npm install --save-dev @types/node This will install node types locally.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download