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 辅助函数;
相关推荐
SUPER52662 小时前
FastApi项目启动失败 got an unexpected keyword argument ‘loop_factory‘
java·服务器·前端
sanx182 小时前
专业电竞体育数据与系统解决方案
前端·数据库·apache·数据库开发·时序数据库
你的人类朋友4 小时前
【Node】认识一下Node.js 中的 VM 模块
前端·后端·node.js
Cosolar4 小时前
FunASR 前端语音识别代码解析
前端·面试·github
@大迁世界7 小时前
Vue 设计模式 实战指南
前端·javascript·vue.js·设计模式·ecmascript
芭拉拉小魔仙7 小时前
Vue项目中如何实现表格选中数据的 Excel 导出
前端·vue.js·excel
jump_jump7 小时前
妙用 localeCompare 获取汉字拼音首字母
前端·javascript·浏览器
U.2 SSD8 小时前
Echarts单轴坐标系散点图
前端·javascript·echarts
德育处主任Pro8 小时前
前端玩转大模型,DeepSeek-R1 蒸馏 Llama 模型的 Bedrock 部署
前端·llama
Jedi Hongbin8 小时前
Three.js NodeMaterial 节点材质系统文档
前端·javascript·three.js·nodematerial