Accessing the File System in Meteor

While working on the impossible-hangman game I stumbled over the problem of accessing the hosts filesystem from Meteor. I intended in earlier versions of the game, to load the dictionary from a file. Meteor sits on top of Node.js, so I wondered if simply using the fs-module, like I would do in plain Node, might solve the problem. Unfortunately, you cannot use require in your app code. There has to be a way to include the module and there was a solution that worked until version 0.6.0 of Meteor:

fs = __meteor_bootstrap__.require 'fs'

But the more recent versions of Meteor don’t seem to support this way any longer. Instead they embrace Node’s modules system: npm, but only in packages. So you can not do:

Npm.require 'fs'

in your app code. You have to create an app-private package. This is actually very simple. Create a subfolder called packages and in it, create another folder fs. In the latter folder you have to put a package.js file and a file providing the sources for the packages. The following gist shows the package definition. There is a little overhead, though, because I’m using CoffeeScript for the package’s source code. Absolutely unnecessary, actually:

The next gist shows the code of

Now I can access fs and path in my apps standard code files. Keep in mind though, that this won’t work if you plan to deploy your app to I guess some security restrictions are in place there. Here is an example of using both modules an app code:

One thought on “Accessing the File System in Meteor

  1. Fret says:

    Hi, you can not only use Npm.require in packages but also under the server folder, and anywhere in a Meteor.isServer block. And last thing is, you can use it in your iron-router if you declare where:server. Next I’ll give an example for the correct way of moving or streaming an uploaded image from tmp folder to a custom folder and read its info with imagemagick ->
    @route ‘route’,
    path: ‘/cropin’
    where: ‘server’
    action: ->
    fs = Npm.require(‘fs’)
    base = process.env.PWD
    img = @request.files.img
    .source = fs.createReadStream(img.path)
    dest = fs.createWriteStream(base+”/public/img/tmp/”

    source.on ‘end’, ->
    console.log(“file #{} moved to /img/tmp/#{}”)
    source.on ‘error’, (err) ->

    info = Imagemagick.identify(base+”/public/img/tmp/”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s