typescript - 找不到模块 'fs' 或其相应的类型声明

标签 typescript fs

我无法让 typescript 识别“fs”模块。 我收到以下错误:

    Error: src/app/components/drops/drops-map/drops-map.component.ts:9:29 - error TS2307: Cannot find module 'fs' or its corresponding type declarations.

9 import {readFileSync}  from 'fs';

我通过以下方式安装了定义:

 npm i @types/node --save-dev

我检查过并且 fs.d.ts 正确地放置在 node_modules/@types/node 文件夹中

这是我的 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ],
    "typeRoots": [
      "node_modules/@types",
      "node_modules/@types/node"
    ],
  },
  "angularCompilerOptions": {
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

我删除了 node_modules 并使用 npm install 重新安装,但没有成功。

最后这是我的 package.json

{
  "name": "myapp",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~13.0.0",
    "@angular/cdk": "^13.0.2",
    "@angular/common": "~13.0.0",
    "@angular/compiler": "~13.0.0",
    "@angular/core": "~13.0.0",
    "@angular/fire": "^7.2.0",
    "@angular/flex-layout": "^12.0.0-beta.35",
    "@angular/forms": "~13.0.0",
    "@angular/google-maps": "^13.0.2",
    "@angular/material": "^13.0.2",
    "@angular/platform-browser": "~13.0.0",
    "@angular/platform-browser-dynamic": "~13.0.0",
    "@angular/router": "~13.0.0",
    "@googlemaps/markerclustererplus": "^1.2.8",
    "@zxing/browser": "^0.0.10",
    "@zxing/library": "^0.18.6",
    "@zxing/ngx-scanner": "^3.3.0",
    "firebase": "^9.4.0",
    "rxfire": "^6.0.0",
    "rxjs": "~7.4.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.0.2",
    "@angular/cli": "~13.0.2",
    "@angular/compiler-cli": "~13.0.0",
    "@types/google.maps": "^3.46.1",
    "@types/jasmine": "~3.10.0",
    "@types/node": "^12.20.37",
    "jasmine-core": "~3.10.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.7.0",
    "typescript": "~4.4.3"
  }
}

请帮忙

最佳答案

您缺少的是 tsconfig.json。您需要将 node 添加到 typescript 编译器选项中的类型:

{  
  "compilerOptions": {
    "types": ["node"]
  }
}

这样您就可以访问节点库了。

关于typescript - 找不到模块 'fs' 或其相应的类型声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70070453/

相关文章:

typescript 装饰器构造函数覆盖包括附加参数

javascript - 通过搜索单词来读取 txt 文件,并检索找到的单词的行数并计算找到的单词的行数 - 可能吗?

node.js - WebStorm tsconfig.json 似乎没有更新正确的文件

node.js - 在 Node.js 中使用 Promise 创建并比较两个数组

javascript - 为什么我的fs错误在Node.js中返回空对象?

javascript - Node fs (文件流) 上传单个文件时创建 2 个文件

javascript - 使用存储查找字符串的数组和存储替换字符串的数组替换文件中的字符串

javascript - 如何在动态生成的mat-table中将字符串转换为类型日期

typescript - 如何使用 ts-node 运行 Protractor 配置文件 (.ts)?

typescript - "supplied parameters do not match any signature of call target"是什么意思?