Meteor + Electron - filesystem (fs)


#1

I’m trying to do something (I think) very simple. I have my app running with meteor and angular 2 on electron, and I would like to use the fs module to read and write files to my desktop… I need it on the client side so a user could click a button and create the file/folder

I seem to run into problems with something like:
fs.writeFileSync(filepath, “HelloWorld”);

Where meteor is saying that fs is not defined (I added a typescript definition but that didn’t help). I’ve tried various solutions but I just get errors, e.g.:

So far nothing is working. Could someone help me find the simplest solution to this (very simple) problem?


#2

Anyone?


#3

var fs = Npm.require('fs'); is no longer working on the latest version of meteor. Try this instead: import fs from 'fs';


#4

Hey Alberto,

Thanks for your reply. I’m out at the moment so will have to try this in a bit but will this work in my angular component on the client side too?


#5

Hey, I just get:

Cannot find module ‘fs’.


#6

I see. I’m not sure but I guess fs doesn’t work on client side. I’ve just tried importing it on my client side but I think it only works on the server.


#7

I had this working before I started using Meteor so I think there must be a way.
The way I got it to work before Meteor was adding the following script tag in my index.html:

  <script> 
      window.fs = require('fs'); 
  </script>

#8

What version of meteor and what feature of fs module are you using?


#9

Meteor 1.3.4.1

And fs is bundled with node so Meteor would determine the fs version right?


#10

core node fs module works on the Server only. If you want to use it with your Electron application you should call fs from Electron Main Process module. You should note that Electron (usually) works as Node (Meteor) backend called Main process + WebKit browser called Render process. In common your browser modules (client) and node backend (server) could talk each other with events (see electron ipc).


#11

thanks for your reply. I didn’t quite get how to interact between the client and server in meteor but i finally got it to work. This is now my startup method (server-side):

Meteor.startup(function () {
    Meteor.methods({
      'writeFileTest': function () {
        console.log('write file test success!');
        fs.writeFileSync('/path/to/file', "HelloWorld");
      }
    });
});

and was able to call it from my angular component with:

Meteor.call('writeFileTest',function(err, res) {
    console.log(err);
    console.log(res);
});

I get a complaint about “Cannot find name ‘fs’” but I guess thats a typescript thing and I can live with that. Thanks for all the help!


#12
import fs from 'fs';
// or I guess typescript/coffescript
// const fs = require('fs');
Meteor.startup(function () {
    Meteor.methods({
      'writeFileTest': function () {
        console.log('write file test success!');
        fs.writeFileSync('/path/to/file', "HelloWorld");
      }
    });
});

#13

Weirdly that actually breaks it! If I add that import and try to call the method I get:

"Internal server error [500]"

So i’ve removed that line and I can live with the warning (Cannot find module ‘fs’)… I’m not sure exactly whats happening though, or how its actually working…

Could it be to do with the load order? currently this startup method is in my main.ts, should I add my meteor method to a lib folder to get it to load later??


#14

Hmmm, it should work. I guess it could be because of your project structure. Can you share your project?


#15

I won’t have access to my project for a few hours but it was built by following the angular2 meteor (socially) app so has the same structure.
The relevant part is that I have a “server” folder in my root then “main.ts” inside that which contains the code I posted before