简体   繁体   English

Visual Studio Code、NodeJS 和 require

[英]Visual Studio Code, NodeJS and require

I installed Visual Studio Code and would like to code NodeJS using TypeScript.我安装了 Visual Studio Code 并想使用 TypeScript 编写 NodeJS。 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.执行提到的npm命令不能解决问题。 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/ 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:只是为了测试我将配置文件tsconfig.json替换为:

{
  "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.我将我的.ts文件移动到项目根文件夹的src文件夹,并创建了dest文件夹。 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.我知道我不能只在 TypeScript 中“要求”,因为它想检查函数是否确实存在,这与 JavaScript 不同。 But how do I actually compile the TypeScript code to JavaScript and then run it?但是我如何将 TypeScript 代码编译成 JavaScript 然后运行它呢? 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:但是 require('crypto') 抛出:
lib.dom.d.ts(18699, 13): 'crypto' was also declared here.

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.如果它不起作用,或者开始一个新项目不是一种选择,你可以尝试在线查找并最终在 Stack Overflow 上发布答案,如果结果不满足你,但你也可以尝试开始“更多新鲜”通过重新安装 node/typescipt & Co.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM