ponyfoo.com

Publishing Node.js Packages with npm

Fix
A relevant ad will be displayed here soon. These ads help pay for my hosting.
Please consider disabling your ad blocker on Pony Foo. These ads help pay for my hosting.
You can support Pony Foo directly through Patreon or via PayPal.

Back when I introduced assetify, I mentioned publishing packages on npm is very easy.

The first thing you have to do is identify yourself, create an account on npm:

$ npm adduser

After that all you have to do is code up a package.json, which essentially requires a package name and a version string.

Here’s an example package.json, extracted from jsn:

{
  "name": "jsn",
  "description": "JavaScript Node server-side variable parser",
  "author": {
    "name": "Nicolas Bevacqua",
    "email": "nicolasbevacqua@gmail.com",
    "url": "http://www.ponyfoo.com"
  },
  "version": "0.0.3",
  "dependencies": {
  },
  "devDependencies": {
    "vows": "0.5.x",
    "mocha": "*",
    "should": "*"
  },
  "repository": {
    "type": "git",
    "url": "git://github.com/bevacqua/jsn.git"
  },
  "bugs": {
    "url": "https://github.com/bevacqua/jsn/issues"
  },
  "licenses": [{
    "type": "MIT"
  }],
  "main": "./src/compiler.js",
  "engines": {
    "node": ">= 0.8.x",
    "npm": ">= 1.1.x"
  }
}

The only thing that might be worth mentioning is that you should make sure to specify a "main" property, so that the module gets exported appropriately through the entry point you intended.

Always make sure to thoroughly test the logic in your packages before unleashing them into the wild. If you happen to publish a faulty image of your package, you can overwrite it using the --force flag:

$ npm publish --force

In addition to .gitignore, .npmignore files can come in handy, here is an example, again from the jsn repository on GitHub.

*.md
.DS_Store
.git*
.idea
Makefile
docs/
examples/
support/
test/

Spread the word!

Liked the article? Subscribe below to get an email when new articles come out! Also, follow @ponyfoo on Twitter and @ponyfoo on Facebook.
One-click unsubscribe, anytime. Learn more.

Comments