Lukas Lukac
Lukas Lukac

Reputation: 8376

meteor.js and npm require fs

I am confused, i need to use fs package for meteor.js fw.

From meteor version 0.6> i need to use Npm.require like this:

var fs = Npm.require('fs');

But when i do it an error appear: npm is not defined

How to solve it? I tried mrt add npm but hm...

BTW: i have /root/packages/npm

EDIT My code was in the both client/server side folder so i moved it to the block for a server as

var fs;
if(Meteor.isServer) {
  fs = Meteor.require('fs');
}

fs.writeFile(path + name,...

GETTING ERROR: Cannot call a method writeFile of undefined

SOLVED Well i solved the error by wraping the whole content to the Meteor.isServer {... but if someoen could explain to me from curiosity why it does not work like above?

Upvotes: 4

Views: 9783

Answers (4)

Joshua Michael Calafell
Joshua Michael Calafell

Reputation: 3107

Not to detract, but... Another option you might want to use is the Meteor file structure itself. You could bypass NPM and use your Assets in Meteor. In Meteor, things in your private folder can be accessed by something like var data = Assets.getText("example.txt").toString().split("\n"); for example, if you wanted to turn a document into an array of word. That's just an example. I'm not sure exactly what you need to do. :)

Upvotes: 0

Andrew Mao
Andrew Mao

Reputation: 36940

You need to add a package.js in your app or a smart package that explicitly specifies the dependency via Npm.depends before you can use Npm.require. You don't need the Npm.depends or a smart package if you are using a built-in npm package such as fs, but you still need to make sure you are using it on the server-side and not the client side.

For an example, check out the package.js file for my Meteor package that pulls in ShareJS: https://github.com/mizzao/meteor-sharejs/blob/master/sharejs-ace/package.js

See also this post: http://shiggyenterprises.wordpress.com/2013/05/16/accessing-the-file-system-in-meteor/

Upvotes: 2

thejinx0r
thejinx0r

Reputation: 471

Since "fs" is part of node, you can simply do: var fs = Meteor.require('fs');

Upvotes: -1

Hubert OG
Hubert OG

Reputation: 19544

It's Npm, not npm, and in your question you use both. Javascript is case-sensitive, make sure you use the proper Npm form.

Upvotes: 2

Related Questions