I cannot use nodeJS global modules inside my angular 8 application.Example "fs" module.I want perform writeFile and readFile operations by using nodeJS 'fs' module. I have tried so many ways,but cannot access those modules in my angular 8 application.Please help me out anyone...
-
Share the code that you have tried. – Yash Aug 29 '19 at 10:46
-
3Angular runs in the browser and not on the server. Therefore there is no access to the file system. – pzaenger Aug 29 '19 at 10:49
2 Answers
If you are developing Electron app with Angular, some work must be applied to access node api.
Install ngx-electron and inport NgxElectronModule (Follow the
Installation
in readme file).Inject ElectronService on the place you need to call node api.
import the package you required via
this._electronService.remote.require(string)
.
Here is a sample to get file list in D:\
via node api.
const fs = this.electronService.remote.require('fs');
console.log(fs.readdirSync('D:\\'));

- 61
- 8
-
sorry,I am not developing angular with electron app.I am developing a web app and i want to access node APIs inside my angular web app. – munishK Aug 29 '19 at 12:55
-
@munishK This is impossible since web broswers never allow websites to access node api – EdgeNeko Aug 29 '19 at 12:56
-
then how to access "fs" inside angular app?because i want to write file from web app to my system.is there any other way to achieve this? – munishK Aug 29 '19 at 13:00
-
This is not something a website can do and rightly so. See this answer for details on how the two JavaScript runtimes (node/Chromium) differ: https://stackoverflow.com/questions/23959868/differences-between-node-environment-and-browser-javascript-environment – hevans900 Aug 29 '19 at 14:44
-
What you can do, is trigger a file to download. In Angular this is pretty easy: https://stackoverflow.com/questions/35138424/how-do-i-download-a-file-with-angular2 – hevans900 Aug 29 '19 at 14:46
-
I know the author was not looking for help within the context of Electron, but this is exactly what I've been looking for. Thanks for the share! – tmptplayer Jan 29 '20 at 21:09
first of all you cant use the nodejs libraries in Angular since it's working on client browser not in server.there are few third party NPM packages which are written for nodejs can be used in angular(eg:auth2,jwt) but you need to add typing's for that in angular.
so make sure that you are not using the system accessing node packages in angular since angular will have control only within the browser.

- 31
- 4
-
can you please elaborate,how to add typings inside angular app? because i tried and also installed "@types/node" but still cannot configure it correctly.So,please help me out for this. – munishK Aug 29 '19 at 12:58
-
for every npm package the authors will themselves create the typing and you can download it via NPM commands but the problems is only few javascript npm libraries have these typings. – arvind AK Aug 29 '19 at 13:18