TS踩坑日记 - error TS2584: Cannot find name ‘console‘. Do you need to change your ……

  • 在使用 ts-node 运行 ts 文件时出现问题

  • test.ts文件:

    ts 复制代码
    const arr: Object[] = [{}, 1];
    const arr1: Array<Object> = [{}, 1, 2];
    console.log(arr, arr1);
  • 执行

    • ts-node ./test.ts
  • 报错

    php 复制代码
    error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.
    
    2   console.log(arr, arr1);
        ~~~~~~~
    
        at createTSError (C:\Users\ASUS\Desktop\crm\ydemo\node_modules\ts-node\src\index.ts:820:12)
        at reportTSError (C:\Users\ASUS\Desktop\crm\ydemo\node_modules\ts-node\src\index.ts:824:19)
        at getOutput (C:\Users\ASUS\Desktop\crm\ydemo\node_modules\ts-node\src\index.ts:1014:36)
        at Object.compile (C:\Users\ASUS\Desktop\crm\ydemo\node_modules\ts-node\src\index.ts:1322:43)       
        at Module.m._compile (C:\Users\ASUS\Desktop\crm\ydemo\node_modules\ts-node\src\index.ts:1454:30)    
        at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
        at Object.require.extensions.<computed> [as .ts] (C:\Users\ASUS\Desktop\crm\ydemo\node_modules\ts-node\src\index.ts:1458:12)
  • 原因console不属于 ECMAScript 标准。DOM里面的 console 是浏览器环境下的(是 window 对象里面的),属于浏览器 BOM APINode 里面的 consoleNodeJS 里面的,由 NodeJS 自己定义的API,两者虽然由同样的功能,但是并不是同一个东西;

  • 解决

    • 安装插件:
      • npm i -D tslib @types/node
    • 该命令是安装 TypScript 助手的运行时库,包含所有 TypeScript 辅助函数;
相关推荐
董员外10 小时前
LangChain.js 快速上手指南:Tool的使用,给大模型安上了双手
前端·javascript·后端
用泥种荷花10 小时前
【LangChain.js学习】 RAG(检索增强生成)完整实现解析
前端
兔子零102411 小时前
Star-Office-UI-Node 实战:从 0 到 1 接入 OpenClaw 的多 Agent 看板
前端·ai编程
helloweilei11 小时前
一文搞懂Nextjs中的Proxy
前端·next.js
wuhen_n11 小时前
Pinia状态管理原理:从响应式核心到源码实现
前端·javascript·vue.js
陆枫Larry11 小时前
小程序 scroll-view 设置 padding 右侧不生效?用一层包裹解决
前端
晴殇i12 小时前
CommonJS 与 ES6 模块引入的区别详解
前端·javascript·面试
Selicens12 小时前
git批量删除本地多余分支
前端·git·后端
wuhen_n12 小时前
KeepAlive:组件缓存实现深度解析
前端·javascript·vue.js
前端付豪12 小时前
Nest 项目小实践之图书展示和搜索
前端·node.js·nestjs