1

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.

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: 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.

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This got rid of the squigglies but I feel like it should be automated. Is there maybe a setting (tsconfig.json or similar) that would make VSCode take care of this automatically? I really don't know much about Typescript.
I guess you can install the types globally. However I find in most cases its better to have types versioned locally as a project specific dependency.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.