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

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

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