Angular 7 how to use fs module? Angular 7 how to use fs module? typescript typescript

Angular 7 how to use fs module?


'fs' is a library for NodeJS runtime, but the end product of the Angular pipeline bundler is a frontend SPA client which runs in a browser. If you need to load a file into the client from the user's local filesystem then you need to use browser native variant e.g. take a look at handling <input type="file"> elements.


fs module is not available in Angular. Use the below package to use fs module.

npm install file-system --save

Example:

var fs = require('file-system'); fs.mkdir('1/2/3/4/5', [mode], function(err) {});fs.mkdirSync('1/2/3/4/5', [mode]);fs.writeFile('path/test.txt', 'aaa', function(err) {})

Reference: https://www.npmjs.com/package/file-system


The 'fs' module is a node module which is not available in Angular (or any other framework for that matter). What happens is that the Angular code is transpiled and minified and runs in the browser context, not in the node context. Browsers are sandboxed and cannot access the client file system. If this were possible than the web would be a lot more dangerous :).