17

Hello i'm facing this issue here: Typescript Node.js simplest setup doesn't work -- error TS2307: Cannot find module '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

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

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Pedro Tainha
  • 497
  • 2
  • 6
  • 14

5 Answers5

16

This solved the issue for me:

npm install --save-dev @types/node
Jaacko Torus
  • 796
  • 7
  • 24
user3093605
  • 169
  • 1
  • 3
  • ehm, surely it works but where do execute this command? I mean.. If open a terminal and just type that, it throws the following error `saveError ENOENT: no such file or directory` – José Crespo Barrios Oct 07 '20 at 08:16
  • 1
    `npm install @types/node --save-dev` was enough for me. It has the advantage of avoiding runtime bloat. – Jonathan Jun 08 '21 at 06:16
5

I found the solution:

import { writeFileSync, readFileSync } from 'fs';

it works now

Pedro Tainha
  • 497
  • 2
  • 6
  • 14
3

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:

{
  "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
    Still not working after adding, getting the same error error TS2307: Cannot find module 'fs'. "typeRoots": [ "node_modules/@types", "node_modules/@types/node" ], – Randhir Singh Jan 27 '20 at 07:46
  • adding the typeRoots, but also actually running `npm i -D @types/node` worked for me – manonthemat Apr 11 '20 at 16:54
  • This worked for me, especially in a monorepo with a root-level tsconfig which is extended by all packages - I was able to add this to the root tsconfig. Thanks! – Ryan Wheale May 17 '21 at 21:51
0

maybe you can add a module-env path in your .js file like this

module.paths.push('.\node_modules'); 
truth Zheng
  • 71
  • 1
  • 2
-2

Are you running this in Node?

Why not import fs from fs?

Also try require and see what's up

Pistolpete .
  • 1,118
  • 1
  • 8
  • 17
  • 1
    it's inside an angular project. It uses typescript for interpretation. I referenced the issue so it could be in context. That does not work. typescript does not accept and module is not found by webpack when buiding – Pedro Tainha Jan 31 '18 at 17:34