node.js 找不到模块“fs”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48546946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:40:29  来源:igfitidea点击:

Cannot find module 'fs'

node.jsangulartypescript

提问by Pedro Tainha

Hello i'm facing this issue here: Typescript Node.js simplest setup doesn't work -- error TS2307: Cannot find module 'fs'

您好,我在这里遇到了这个问题: Typescript Node.js 最简单的设置不起作用——错误 TS2307:找不到模块 'fs'

And i've tried also that solution but i can't put it working

我也试过那个解决方案,但我不能让它工作

my dependencies:

我的依赖:

"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"
  }

the tsconfig.json

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"
  ]
}

and in the code when i try:

在我尝试时的代码中:

import * as fs from 'fs';

export const environment = {
  production: false,
  hmr: true
};

i get:

我得到:

ERROR in .../src/environments/environment.hmr.ts (1,21): Cannot find module 'fs'.

Any help would be appreciated. Thanks

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

回答by Pedro Tainha

I found the solution:

我找到了解决方案:

import { writeFileSync, readFileSync } from 'fs';

it works now

它现在有效

回答by Jairo Blanco Aldao

I know i'm probably late but i was faceing the same issue. Finally I got a work around if you want to try. In your tsconfig.json add "node_modules/@types/node" in "typeRoots" as you can see in the config bellow:

我知道我可能迟到了,但我遇到了同样的问题。如果您想尝试,我终于找到了解决方法。在您的 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"
    ]
  }
}

回答by user3093605

This solved the issue for me:

这为我解决了这个问题:

npm install @types/node

回答by Pistolpete .

Are you running this in Node?

你是在 Node 中运行这个吗?

Why not import fs from fs?

为什么不 import fs from fs呢?

Also try requireand see what's up

也尝试require看看发生了什么