Typescript Node.js 最简单的设置不起作用 - 错误 TS2307:找不到模块“fs”

2024-01-08

我已经全局安装了 TS 和节点类型:

PS C:\Projects\Test> npm list --global --depth=0
C:\Users\Jan\AppData\Roaming\npm
+-- @types/[email protected] /cdn-cgi/l/email-protection
+-- [email protected] /cdn-cgi/l/email-protection
`-- [email protected] /cdn-cgi/l/email-protection

然后我创建了一个文件 test.ts

import fs = require("fs");
let text = fs.readFileSync("myFile.txt");
console.log(text);

运行 tsc 结果为

PS C:\Projects\Test> tsc .\test.ts
test.ts(1,21): error TS2307: Cannot find module 'fs'.

我错过了一些明显的事情吗?

Thanks!


你应该安装@types本地到您的项目。

npm install @types/node --save-dev

TypeScript 不会找到全局安装的类型作为定义文件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Typescript Node.js 最简单的设置不起作用 - 错误 TS2307:找不到模块“fs” 的相关文章

随机推荐