Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 292
Lukas Lukac Avatar asked Aug 31 '25 05:08

Lukas Lukac


2 Answers

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

like image 110
Hubert OG Avatar answered Sep 02 '25 18:09

Hubert OG


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/

like image 29
Andrew Mao Avatar answered Sep 02 '25 18:09

Andrew Mao