console.log('1'),报错

来源:2-4 TS 代码的执行原理

可达鸭_

2021-08-26

ts 打印个1 直接报错
命令行 输入 ts-node demo.ts

TSError: ⨯ Unable to compile TypeScript:
demo.ts:12:1 - error TS2584: Cannot find name 'console'. Do you need to change your target library? 
Try changing the 'lib' compiler option to include 'dom'.

12 console.log('1')
 at createTSError (C:\Users\wyf\AppData\Roaming\nvm\v14.17.3\node_modules\ts-node\src\index.ts:692:12)
 at reportTSError (C:\Users\wyf\AppData\Roaming\nvm\v14.17.3\node_modules\ts-node\src\index.ts:696:19)
 at getOutput (C:\Users\wyf\AppData\Roaming\nvm\v14.17.3\node_modules\ts-node\src\index.ts:883:36)
 at Object.compile (C:\Users\wyf\AppData\Roaming\nvm\v14.17.3\node_modules\ts-node\src\index.ts:1185:30)
 at Module.m._compile (C:\Users\wyf\AppData\Roaming\nvm\v14.17.3\node_modules\ts-node\src\index.ts:1309:30)
 at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
 at Object.require.extensions.<computed> [as .ts] (C:\Users\wyf\AppData\Roaming\nvm\v14.17.3\node_modules\ts-node\src\index.ts:1313:12)
 at Module.load (internal/modules/cjs/loader.js:950:32)
 at Function.Module._load (internal/modules/cjs/loader.js:790:14)
 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
diagnosticText: "\x1B[96mdemo.ts\x1B[0m:\x1B[93m12\x1B[0m:\x1B[93m1\x1B[0m - \x1B[91merror\x1B[0m\x1B[90m TS2584: \x1B[0mCannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.\r\n" +
 '\r\n' +
 "\x1B[7m12\x1B[0m console.log('1')\r\n" +
 '\x1B[7m  \x1B[0m \x1B[91m~~~~~~~\x1B[0m\r\n',
diagnosticCodes: [ 2584 ]
}

node 版本v14.17.3
ts-node版本 v10.2.1

然后tsc 编译 node在运行js就可以图片描述

写回答

1回答

Dreamer1017

2021-08-26

tsconfig.json文件的compilerOptions里的lib添加一项“DOM“


//img.mukewang.com/szimg/6127491d09554e1111160522.jpg

0
2
可达鸭_
非常感谢!
2021-08-27
共2条回复

专为小白设计的TypeScript入门课

Dell老师专为TypeScript小白打造的,全栈式教学TS入门课程

2249 学习 · 506 问题

查看课程