ts-node使用过程中报错,两行代码解决

运行时

PS D:\TsStudy> ts-node demo1.ts
C:\Users\xukailin\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:750
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
demo1.ts:3:9 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

3         console.log(`Animal moved ${distanceInMeters}m.`);
          ~~~~~~~
demo1.ts:9:9 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

9         console.log('Woof! Woof!');
          ~~~~~~~

    at createTSError (C:\Users\xukailin\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:750:12)
    at reportTSError (C:\Users\xukailin\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:754:19)
    at getOutput (C:\Users\xukailin\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:941:36)
    at Object.compile (C:\Users\xukailin\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1243:30)
    at Module.m._compile (C:\Users\xukailin\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1370:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
  diagnosticText: "\x1B[96mdemo1.ts\x1B[0m:\x1B[93m3\x1B[0m:\x1B[93m9\x1B[0m - \x1B[91merror\x1B[0m\x1B[90m TS2584: \x1B[0mCannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.\r\n" +
    '\r\n' +
    '\x1B[7m3\x1B[0m         console.log(`Animal moved ${distanceInMeters}m.`);\r\n' +
    '\x1B[7m \x1B[0m \x1B[91m        ~~~~~~~\x1B[0m\r\n' +
y? Try changing the 'lib' compiler option to include 'dom'.\r\n" +
    '\r\n' +
    "\x1B[7m9\x1B[0m         console.log('Woof! Woof!');\r\n" +
    '\x1B[7m \x1B[0m \x1B[91m        ~~~~~~~\x1B[0m\r\n',
  diagnosticCodes: [ 2584, 2584 ]
}

原因是就目前来说安装的tsnode版本过新卸载后,重新安装个低版本的就好了

使用 npm uni -g ts-node 卸载掉ts-node

再使用 npm i -g ts-node@8.5.4下载旧一点的版本就不会了. 
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容