About package system in MeteorJS Meteor JS 20.01.2016

One of the most powerful aspects of Meteor is its active ecosystem of packages. All applications you build with Meteor, even the simplest "Hello World" example, already rely on dozens of packages.

Packages can be categorized into three types:

  • Isopacks, which are Meteor’s own package format
  • Cordova packages, which provide mobile functionality
  • NPM packages, which are Node.js packaged modules

Because of their isomorphic nature, Meteor packages are called Isopacks. In contrast to npm modules, they aren’t limited to the server, but they can include server, browser, and even mobile code.

Meteor is built on top of Node.js, so it’s possible to use all packages for Node.js as well. npm manages those packages. Its repository contains more than 100,000 packages. A huge community of JavaScript developers has created packages for almost any use case and it’s simple to integrate them into Meteor projects as well.

There are two ways to add an npm package to a project. The first is to wrap it into a Meteor package. The second approach is to use the meteorhacks:npm package, which allows you to use a packages.json similar to plain Node.js projects.

Let’s start by adding the required Meteor package:

meteor add meteorhacks:npm

The package enhances Meteor applications so that npm modules can be used directly. Because the package needs to perform some configuration tasks before it can add modules, the project must be run using the meteor run command after you add meteorhacks:npm. As a result, a new folder named packages will be added to the project. It contains an npm-container package that will take care of adding npm modules. To specify which module you want to add to a project, you’ll use a packages.json file. This file is also created when first running a Meteor project with the npm package added, and it’s located at the root of the application folder.

As shown below, all modules that should be added to the application are listed as keys and the required versions as values. I'm using the cheerio module as an example.

{
    "cheerio": "0.19.0"
}

Once a module is loaded, it can be used in the same way you would in a plain Node.js application.

var cheerio =  Meteor.npmRequire('cheerio')
r = HTTP.get(url)
$ = cheerio.load(r.content)

or example with fs

var fs =  Meteor.npmRequire('fs')
fs.unlink(path)