最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - meteor.js and npm require fs - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Feb 15, 2014 at 23:02 Gilles 'SO- stop being evil' 108k38 gold badges215 silver badges260 bronze badges asked Jul 11, 2013 at 15:19 Lukas LukacLukas Lukac 8,37610 gold badges69 silver badges75 bronze badges 4
  • Could you run mrt --version and check your meteor version? – Lorenzo Marcon Commented Jul 11, 2013 at 15:30
  • 3 additional question for you: are you trying to use Npm server or client side? (you can just use it server side) – Lorenzo Marcon Commented Jul 11, 2013 at 15:33
  • I had run mrt --version before i posted question. It was 0.6.4 i guess. And in collections folder i am not sure what side it is? – Lukas Lukac Commented Jul 11, 2013 at 22:31
  • Code outside Meteor.isServer block gets executed also on the client, and since 'fs' isn't defined there it throws error. – JussiR Commented Aug 21, 2013 at 10:28
Add a ment  | 

4 Answers 4

Reset to default 2

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

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./mizzao/meteor-sharejs/blob/master/sharejs-ace/package.js

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

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. :)

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

发布评论

评论列表(0)

  1. 暂无评论