こんにちは、私はここでこの問題に直面しています: 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'.
任意の助けをいただければ幸いです。ありがとう
私は解決策を見つけました:
import { writeFileSync, readFileSync } from 'fs';
今すぐ動作します
私はおそらく遅れていることは知っていますが、同じ問題に直面していました。最後に、試してみたい場合は回避策があります。 tsconfig.jsonで、以下の構成で確認できるように、「typeRoots」に「node_modules/@ types/node」を追加します。
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types",
"node_modules/@types/node"
],
"lib": [
"es2018",
"dom"
]
}
}