我无法让 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/