node.js - 找不到模块 'fs'

标签 node.js angular typescript

您好,我遇到了这个问题: Typescript Node.js simplest setup doesn't work -- error TS2307: Cannot find module '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'.

如有任何帮助,我们将不胜感激。谢谢

最佳答案

这为我解决了这个问题:

npm install --save-dev @types/node

关于node.js - 找不到模块 'fs',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48546946/

相关文章:

angular - 我需要 Angular2 中的构造函数主体吗?

javascript - React Component 成员在渲染后重置为未定义

node.js - 从 API 响应中打开 PDF 到 Angular 中的新选项卡

javascript - 如何检测脚本是在浏览器中还是在 Node.js 中运行?

Angular 5 : Unit testing getters and setters

angular - Bootstrap Navbar 无法正常工作 : AngularCLI

javascript - 从 gulp typescript js 流中分割 src 和测试文件

node.js - 如何从数据事件中取消 HTTP 上传?

javascript - 连接两个集合并在 Mongoose 中对它们进行排序

node.js - 无法在 Angular 2 应用程序中提供图像