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

推荐订阅源

The GitHub Blog
The GitHub Blog
Martin Fowler
Martin Fowler
Vercel News
Vercel News
U
Unit 42
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
N
Netflix TechBlog - Medium
GbyAI
GbyAI
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
M
MIT News - Artificial intelligence
D
Docker
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog

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