你好,我遇到了这个问题:
Typescript Node.js最简单的设置无法工作--错误TS2307:找不到模块 'fs'我已经尝试了那个解决方案,但是无法使它正常工作。
我的依赖项:
"dependencies": {
"@angular/animations": "^4.0.0",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"@swimlane/ngx-charts": "^6.1.0",
"core-js": "^2.4.1",
"d3": "^4.9.x",
"mobx": "^3.3.1",
"mobx-angular": "^2.0.0",
"mobx-remotedev": "^0.2.8",
"rxjs": "^5.0.1",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@angular/cli": "1.4.1",
"@angular/compiler-cli": "^4.0.0",
"@angular/language-service": "^4.0.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "^9.4.0",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"tslint-config-prettier": "^1.6.0",
"typescript": "~2.5.3"
}
tsconfig.json文件
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"types": [
"node"
],
"lib": [
"es2017",
"dom"
]
},
"exclude": [
"node_modules",
"dist"
],
"filesGlob": [
"./node_modules/@types/**/*.d.ts"
]
}
在代码中当我尝试:
import * as fs from 'fs';
export const environment = {
production: false,
hmr: true
};
我明白了:
ERROR in .../src/environments/environment.hmr.ts (1,21): Cannot find module 'fs'.
非常感谢您的帮助。
saveError ENOENT: no such file or directory
。 - José Crespo Barriosnpm install @types/node --save-dev
对我来说已经足够了。它的优点是避免了运行时膨胀。 - Jonathan