1

I installed Visual Studio Code and would like to code NodeJS using TypeScript. So I created very basic application and I get error that says:

Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.ts(2580)

Executing mentioned npm command does not resolve the issue. I've lost the wall of links I've followed, read and either didn't apply to me, didn't fix the issue, were outdated etc.:

This one came closest:
https://www.section.io/engineering-education/how-to-use-typescript-with-nodejs/

I ran these commands:
npm i --save-dev @types/node
npm install -g typescript
npm install -D typescript

Just to test I replaced configuration file tsconfig.json with:

{
  "compilerOptions": {                        
    "target": "es6",                               
    "module": "commonjs",                           
    "outDir": "./dist",                             
    "rootDir": "./src",                             
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,                       
  },
  "exclude":[
    "./node_modules"
  ]
}

I moved my .ts file to src folder of the root folder of the project, and created dest folder. I restarted the application, nothing. I looked for extensions which might've helped, nothing.

I understand that I can't just "require" in TypeScript because it wants to check if function actually exists, unlike JavaScript. But how do I actually compile the TypeScript code to JavaScript and then run it? The lines that cause problem are the very three at the topic of the file.

const http = require('http');
const crypto = require('crypto');
const colors = require('colors');

Edit:
declare function require(name : string); Fixed the issue. But require('crypto') throws:
lib.dom.d.ts(18699, 13): 'crypto' was also declared here.

John Doe
  • 13
  • 4
  • Check [this](https://stackoverflow.com/a/35171227/17264570) answer – eroironico Nov 15 '21 at 09:45
  • I already tried that, its mentioned in the post. It doesn't fix anything. – John Doe Nov 15 '21 at 09:50
  • Have you tried using the `import` keyword in your ts source? – DadiBit Nov 15 '21 at 11:38
  • @DadiBit Both `const crypto = import('crypto')` and `import crypto from "crypto"` throw `Cannot find module 'crypto' or its corresponding type declarations.ts(2307)` despite `npm install -g crypto` and a restart. – John Doe Nov 15 '21 at 19:23
  • Renaming variable `crypto` to `xcrypto` gets rid of the message, but Visual Code cannot find the definition of `createHash` (and practically acts like regular JavaScript with no comprehension of code). – John Doe Nov 15 '21 at 19:38
  • I really don't know where the problem could be, honestly... Could you kindly try to create a project from scratch and import only the crypto module after installing node types? This way we might understand if it's a project-only or a global issue :) – DadiBit Nov 15 '21 at 21:37
  • @DadiBit I created a new project, refollowed the project and now it works precisely as expected. I don't understand. The other project was identically empty. I get autocomplete and everything. – John Doe Nov 16 '21 at 12:11
  • Everything works exactly as expected, I don't know what failed previous times but "package.json" and "tsconfig.json" were not visible. But now everything works. Thank you. (If you post it as the answer, I'll accept it). – John Doe Nov 16 '21 at 12:27

1 Answers1

0

Honestly, I don't know precisely what is causing the issue, but I'd guess some configuration is broken.

Sometimes an easy way out is to simply start fresh: try creating a new project and import only the stuff you need.

  • If it works, you should continue developing on the "new project". This sometimes is not an option, but it's probably the easiest way out with these types of problem.

  • If it doesn't work, or starting a new project is not an option, you can try looking up online and eventually post an answer here on Stack Overflow if the results don't satisfy you, but you can also try starting "even more fresh" by reinstalling node/typescipt & Co.

DadiBit
  • 769
  • 10
  • 22