惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Security Latest
Security Latest
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
S
Secure Thoughts
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
D
DataBreaches.Net
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
罗磊的独立博客
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
V
V2EX
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tenable Blog
T
Threat Research - Cisco Blogs
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
F
Full Disclosure
H
Help Net Security
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
O
OpenAI News
S
Securelist

Deed's博客

解决群晖百度套件提示name "baiduapp" dupliacted - Deed's博客 2023年3月16日 Maven项目引入第三方本地jar包 - Deed's博客 2022年8月23日nginx反向代理配置去除前缀 - Deed's博客 2022年7月13日 Gradle项目打包相关配置 - Deed's博客 2022年6月23日 Windows 杀死进程 - Deed's博客 2022年6月16日 查看服务器异常IP访问以及处理 - Deed's博客 2022年6月15日 Linux删除用户 - Deed's博客 2022年6月8日 AntDesignPro文档Demo示例[后置拦截器]报错 - Deed's博客 2022年6月8日 React使用umi.js报错Invalid hook call - Deed's博客
2022年8月1日 ts-node 运行报错`Cannot find name 'console'.`
Mahalalel · 2022-08-01 · via Deed's博客
«

Mahalalel 发布于 阅读:7823 TypeScript


背景

最近小编在学习TypeScript,运行基本demo的时候,遇到一个报错,特此记录下。
异常如下:

D:\develop\nvm\v14.17.0\node_global\node_modules\ts-node\src\index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
demo.ts:15:3 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

15   console.log(a);
     ~~~~~~~

    at createTSError (D:\develop\nvm\v14.17.0\node_global\node_modules\ts-node\src\index.ts:859:12)
    at reportTSError (D:\develop\nvm\v14.17.0\node_global\node_modules\ts-node\src\index.ts:863:19)
    at getOutput (D:\develop\nvm\v14.17.0\node_global\node_modules\ts-node\src\index.ts:1077:36)
    at Object.compile (D:\develop\nvm\v14.17.0\node_global\node_modules\ts-node\src\index.ts:1433:41)
    at Module.m._compile (D:\develop\nvm\v14.17.0\node_global\node_modules\ts-node\src\index.ts:1617:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Object.require.extensions.<computed> [as .ts] (D:\develop\nvm\v14.17.0\node_global\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
  diagnosticCodes: [ 2584 ]
}

问题是demo.ts:15:3 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.;

原因就在于,无法识别console函数。

解决

1、全局安装 ts-node 运行依赖包

命令如下:

npm install -g tslib @types/node

@types/node解释

关于@types,可以参考下 深入理解TypeScript @types

类型定义中,可以看到 @types一个高质量的TypeScript类型定义的仓库

@types/node是TS文件做声明作用的。

tslib解释

npm库中对其定义为这是一个包含所有 TypeScript 辅助函数的 TypeScript运行时库

2、降低ts-node版本

网络上还有一种方法,降低ts-node的版本,可以解决这个问题,

注:此种办法,并未做测试。

TypeScript