找不到模块' fs'

时间:2018-01-31 16:29:56

标签: node.js angular typescript

您好我在这里遇到此问题: init(1)

我也尝试过这个解决方案,但我无法正常工作

我的依赖项:

"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'.

任何帮助将不胜感激。感谢

5 个答案:

答案 0 :(得分:4)

我找到了解决方案:

import { writeFileSync, readFileSync } from 'fs';

现在可以使用

答案 1 :(得分:4)

这为我解决了这个问题:

npm install @types/node

答案 2 :(得分:1)

你是在Node中运行吗?

为什么不呢 import fs from fs

同时尝试require并查看内容

答案 3 :(得分:0)

我知道我可能迟到了,但我面临着同样的问题。最后,如果您想尝试的话,我得到了解决。在您的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"
    ]
  }
}

答案 4 :(得分:0)

也许你可以像这样在你的 .js 文件中添加一个 module-env 路径

module.paths.push('.\node_modules');