Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module 'fs'

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

like image 486
Pedro Tainha Avatar asked Jan 31 '18 16:01

Pedro Tainha


People also ask

Do I need to install fs module?

This module is for handling the file system, as fs stands for the file system. There is no need to install this module, as it is available as a built-in module that comes with the installation of Node.

What is fs node module?

The built-in Node. js file system module helps us store, access, and manage data on our operating system. Commonly used features of the fs module include fs. readFile to read data from a file, fs.

How do I use typescript fs?

To import and use the fs module in TypeScript, make sure to install the type definitions for node by running npm i -D @types/node and import fs with the following line import * as fs from 'fs' , or import { promises as fsPromises } from 'fs' if using fs promises.


2 Answers

This solved the issue for me:

npm install @types/node
like image 123
user3093605 Avatar answered Oct 15 '22 10:10

user3093605


I found the solution:

import { writeFileSync, readFileSync } from 'fs';

it works now

like image 25
Pedro Tainha Avatar answered Oct 15 '22 09:10

Pedro Tainha