<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>David Beath (Posts about nodejs)</title><link>https://davidbeath.com/</link><description></description><atom:link href="https://davidbeath.com/categories/nodejs.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Mon, 21 Feb 2022 17:09:57 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Using Node Modules with Meteor</title><link>https://davidbeath.com/posts/using-node-modules-with-meteor/</link><dc:creator>David Beath</dc:creator><description>&lt;p&gt;After playing with &lt;a href="https://www.meteor.com/"&gt;Meteor&lt;/a&gt; for the past week or so, which overall I really enjoyed, the one thing that really bugged me was how to use &lt;a href="https://nodejs.org"&gt;node.js&lt;/a&gt; modules in a Meteor app. There are plenty of instructions out there that sort of tell you what to do, but I found all of them to be variously confusing and unclear. Of course, this isn't helped by the fact that at the time of writing Meteor is only version 0.6.5.1, so things are still in flux. The aim of this post is therefore to provide a comprehensive and clear guide to how I was able to get Meteor to work with Node modules.&lt;/p&gt;
&lt;!-- TEASER_END --&gt;

&lt;p&gt;The first thing of course is to initialise a new Meteor app. Feel free to skip this step if you're using an existing one.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&amp;gt; meteor create myapp
&amp;gt; &lt;span class="nb"&gt;cd&lt;/span&gt; myapp
&lt;/pre&gt;
&lt;p&gt;Then create a folder called "packages" within the root of your app.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&amp;gt; mkdir packages &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; packages
&lt;/pre&gt;
&lt;p&gt;For each node module or custom package you want to use, create a new folder. For this example I'm going to use &lt;a href="https://github.com/danmactough/node-feedparser"&gt;feedparser&lt;/a&gt;, as that's what I was using.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&amp;gt; mkdir feedparser &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; feedparser
&lt;/pre&gt;
&lt;p&gt;You'll then want to create two javascript files, one called package.js, and the other the name of the module.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&amp;gt; touch package.js
&amp;gt; touch feedparser.js
&lt;/pre&gt;
&lt;p&gt;In package.js, declare the module you want to use, making sure to specify the version like so:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span class="nx"&gt;Package&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;summary&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Reads RSS feeds"&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;Npm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;depends&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;feedparser&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"0.16.1"&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Next, add the following lines, or the equivalent for your requirements, to package.js. You can find more information about what you can do here in the Meteor documentation, and by looking at the &lt;a href="https://github.com/meteor/meteor/tree/master/packages/"&gt;packages directory in the Meteor source tree&lt;/a&gt;. For me, these lines were what I required to access the module within my Meteor app.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span class="nx"&gt;Package&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on_use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add_files&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'feedparser.js'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'server'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;export&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;export&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Feedparser'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Now, just create your variable to export by calling &lt;code&gt;Npm.require()&lt;/code&gt; on it in the other .js file you created.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span class="nx"&gt;Feedparser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Npm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'feedparser'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Finally, in your application root folder, add the package to your app:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&amp;gt; meteor add feedparser
&lt;/pre&gt;
&lt;p&gt;Now Meteor will update the dependencies for the package. Once this is done, you should be able to use the module within your app by calling the api you exported.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Feedparser&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Hopefully now you'll have a working Node module within your Meteor app. You can view the full code for this example &lt;a href="https://github.com/DBeath/meteor-rss/"&gt;here&lt;/a&gt;. If you know a better or easier way to do this, or a more proper way, then please email me to say so.&lt;/p&gt;</description><category>Meteor</category><category>nodejs</category><category>tutorial</category><guid>https://davidbeath.com/posts/using-node-modules-with-meteor/</guid><pubDate>Fri, 20 Sep 2013 09:52:15 GMT</pubDate></item></channel></rss>