【发布时间】:2022-11-05 02:12:58
【问题描述】:
我无法让打字稿识别“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"
}
}
请帮忙
【问题讨论】:
-
从文件名来看,您似乎正在尝试在 Angular 组件中使用
fs模块。除非您仅将fs用于该组件(或类似组件)的服务器端呈现的服务器部分,否则您不能这样做。fs模块在浏览器中不存在,仅在 Node.js 中。 -
我怀疑您在
tsconfig中需要类似"lib":["node"]的东西,但就像T.J.Crowder 提到的那样,如果您打算在客户端运行代码,这似乎不是一个正常的用例。 -
哦,我明白了,谢谢,这解释了一切。 :)
标签: typescript fs