找不到模块'fs'。

17

你好,我遇到了这个问题:

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

非常感谢您的帮助。

5个回答

16
这对我解决了问题:
npm install --save-dev @types/node

嗯,肯定可以工作,但是在哪里执行这个命令呢?我的意思是...如果打开一个终端并只输入它,它会抛出以下错误saveError ENOENT: no such file or directory - José Crespo Barrios
1
npm install @types/node --save-dev 对我来说已经足够了。它的优点是避免了运行时膨胀。 - Jonathan

5
我找到了解决方案:
import { writeFileSync, readFileSync } from 'fs';

现在它可以正常工作了


4
这也不行,文件中没有错误,但代码无法编译。 - RDV

3

我知道我可能有点晚了,但我也遇到了同样的问题。最后,我找到了一个解决方法,如果你想尝试一下。在你的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"
    ]
  }
}

1
添加后仍然无法工作,出现相同的错误 TS2307: 找不到模块 'fs'。 "typeRoots": [ "node_modules/@types", "node_modules/@types/node" ] - Randhir Singh
为我解决了问题的是添加了 typeRoots,同时运行 npm i -D @types/node - manonthemat
这对我很有用,特别是在一个monorepo中,根级别的tsconfig被所有包扩展 - 我能够将其添加到根tsconfig中。谢谢! - Ryan Wheale

0
也许你可以在你的 .js 文件中添加一个 module-env 路径,像这样:
module.paths.push('.\node_modules'); 

-2
你是在 Node 中运行这个程序吗?
为什么不使用 import fs from fs 呢?
同时尝试一下 require 看看会发生什么。

1
它是在一个Angular项目中。它使用TypeScript进行解释。我引用了该问题,以便上下文更清晰。但那并不起作用。TypeScript不接受,当构建时Webpack找不到该模块。 - Pedro Tainha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接