【问题标题】:Cannot find module 'fs'找不到模块“fs”
【发布时间】:2018-07-10 20:17:12
【问题描述】:

您好,我在这里遇到了这个问题: 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'.

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

【问题讨论】:

    标签: node.js angular typescript


    【解决方案1】:

    你是在 Node 中运行这个吗?

    为什么不 import fs from fs?

    也可以试试require 看看发生了什么

    【讨论】:

    • 它在一个 Angular 项目中。它使用打字稿进行解释。我引用了这个问题,所以它可以在上下文中。那是行不通的。 typescript 不接受,构建时 webpack 找不到模块
    【解决方案2】:

    我找到了解决办法:

    import { writeFileSync, readFileSync } from 'fs';
    

    现在可以使用了

    【讨论】:

    • 这个也不行,文件没有错误,但是代码没有编译。
    【解决方案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"
        ]
      }
    }
    

    【讨论】:

    • 添加后仍然无法正常工作,得到相同的错误错误 TS2307: 找不到模块'fs'。 "typeRoots": [ "node_modules/@types", "node_modules/@types/node" ],
    • 添加 typeRoots,但实际上也运行 npm i -D @types/node 为我工作
    • 这对我有用,特别是在一个具有根级 tsconfig 的 monorepo 中,该根级 tsconfig 由所有包扩展 - 我能够将它添加到根 tsconfig。谢谢!
    【解决方案4】:

    这为我解决了这个问题:

    npm install @types/node
    

    【讨论】:

    • ehm,当然可以,但是在哪里执行这个命令呢?我的意思是..如果打开一个终端并输入它,它会抛出以下错误saveError ENOENT: no such file or directory
    • npm install @types/node --save-dev 对我来说已经足够了。它具有避免运行时膨胀的优点。
    【解决方案5】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-12
      • 2017-09-06
      • 2019-06-11
      • 2021-08-22
      • 2021-09-06
      • 2021-07-15
      • 2019-10-19
      • 2022-06-27
      相关资源
      最近更新 更多