ts-node使用报错:Cannot find name 'console'

ts-node 是一个可以直接运行 ts 文件的 npm 工具包。如果是初次使用,直接运行 typescript 代码可能会遇到这种报错:Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.。本文介绍一下两种解决方法。

ts-node

一、完整报错

D:\dev\node\node_modules\ts-node\src\index.ts:820
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
demo.ts:11:1 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

11 console.log(n)
   ~~~~~~~

    at createTSError (D:\dev\nvm\v12.5.0\node_modules\ts-node\src\index.ts:820:12)
    at reportTSError (D:\dev\nvm\v12.5.0\node_modules\ts-node\src\index.ts:824:19)
    at getOutput (D:\dev\nvm\v12.5.0\node_modules\ts-node\src\index.ts:1014:36)
    at Object.compile (D:\dev\nvm\v12.5.0\node_modules\ts-node\src\index.ts:1322:43)
    at Module.m._compile (D:\dev\nvm\v12.5.0\node_modules\ts-node\src\index.ts:1454:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Object.require.extensions.<computed> [as .ts] (D:\dev\nvm\v12.5.0\node_modules\ts-node\src\index.ts:1458:12)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:839:10) {
  diagnosticCodes: [ 2584 ]
}

二、解决方法

以下两种方法任选其一即可:

  1. ts-node 版本过高,降低一下版本就好了。
npm i -g ts-node@8.5.4
  1. 全局安装 ts-node 运行依赖包 tslib@types/node
npm i -g tslib @types/node

欢迎访问:天問博客

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容