0

I am trying to use unlink function to delete a file from local system.but getting error that cannot find module 'fs'.. following are the files to give u some insights. In my

app.component.ts

import * as fs from 'fs';
export class Component {
 var filepath = 'C:/Users/[username]/Downloads/test.txt';
 fs.unlink(filepath);

}

but is gives error that cannot find module 'fs'

 package.json
{
"name": "view-evidence-app",
"version": "0.0.0",
"license": "MIT",
 "scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --dev",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"@types/node": "^6.0.60",
"@types/xlsx": "0.0.36",
"angular-file-saver": "^1.1.3",
"angular-font-awesome": "^2.0.3",
"core-js": "^2.4.1",
"file-saver": "^1.3.8",
"file-saver-typescript": "^1.0.1",
"file-system": "^2.2.2",
"file-url": "^2.0.2",
"font-awesome": "^4.7.0",
"fs": "0.0.1-security",
"path": "^0.12.7",
"primeng": "^5.2.7",
"rxjs": "^5.5.6",
"ts-xlsx": "0.0.11",
"zone.js": "^0.8.19"
 },
"devDependencies": {
"@angular-devkit/build-angular": "~0.6.8",
"@angular/cli": "^6.0.8",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "^6.0.60",
"angular-ide": "^0.9.39",
"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",
"typescript": "~2.5.3"
 }
 }

 webpack.config.js

 var path = require("path")
 const nodeExternals = require('webpack-node-externals');
 var webpack = require('webpack');

 module.exports = {
"entry": "./lib/index.js"
 ,   "output": {
    "path": __dirname + "/build"
 ,  "filename": "xxhash.js"
 ,  "library": "XXH"
 ,  "libraryTarget": "umd"
 }
 ,   "target": "node"

 ,"node": {
  "fs": "empty",
 }  
 ,    "externals":{
        "fs":    "commonjs fs",
        "path":  "commonjs path"
    }
    }

I have added line in webpack.config.js

  var path = require("path")
  const nodeExternals = require('webpack-node-externals');//added
  var webpack = require('webpack');//added

   module.exports = {
  "entry": "./lib/index.js"
  ,   "output": {
    "path": __dirname + "/build"
   ,    "filename": "xxhash.js"
  , "library": "XXH"
    ,   "libraryTarget": "umd"
    }
  ,   "target": "node"     //added

   ,"node": {         //added
  "fs": "empty",
    }   
   ,      "externals":{           //added
        "fs":    "commonjs fs",
        "path":  "commonjs path"
      }
     }

links which i refered:

Cannot find module 'fs'

cannot-find-module-fs-when-using-webpack

Uncaught Error: Cannot find module “fs” when using webpack

Akshay Indalkar
  • 359
  • 2
  • 12
  • I think u are trying to delete local system file from browser ( angular ). That is not possible due to security risks involved. – Dhyey Jun 22 '18 at 09:46
  • but then error should come at run time , i am getting an error at compile time, whats the reason for that.@Dhyey – Akshay Indalkar Jun 22 '18 at 10:30
  • the library `fs` and `file-system` which u are trying to use are both made for use in `node` and not angular, that's why u are getting compile time error – Dhyey Jun 22 '18 at 11:54
  • yeah, both library is of node , so how to use that in my program..i have node.js installed – Akshay Indalkar Jun 22 '18 at 12:12

1 Answers1

0

Use file-system npm module for file system usage fs module seems to be deprecated.

npm install file-system --save

import * as fs from 'file-system';

Hope it will work!

eduPeeth
  • 1,840
  • 13
  • 21
  • I have used this also, but this does not any method to delete a file, and when i used filesystem.write(), it contains internally 'fs' only and error comes 'fs' cannnot be resolved.@eduPeeth – Akshay Indalkar Jun 22 '18 at 11:32