Publishing my first npm package
So today was a good day! I published my first npm package. I’ll admit, it’s not the best package out there but I’m happy with what I have until now and I had the chance to learn how to publish a package to the npm registry.
The package is called connect-routing and, as you can tell from the name, is a connect routing middleware. Check out the package here or view the source code here
But I’ll stop there as this blog post is about npm and how you can publish your own package.
Npm
Npm stands for node package manager, and as the name says it’s a package manager used mostly for node modules. To publish you own node module you need to start by having a package.json file.
package.json
This file is used to add certain metadata to your package like version, name, description but also to define dependencies and the main file for running your package.
The four needed properties are name, version, author and main.
- version - Pretty self explanatory
- name - Your awesome pakcgae needs a cool name (how about magic squirrel?)
- author - Who created this thing that kicks ass?
- main - Your main project/library file that makes the squirrel magic
Great so now that we have the final source for our project and a minimal package.json what do we do?
npm adduser
Next up we need to have a user registered, so to do that we use npm adduser. This will prompt us for a username, password and an email address that will be public.
npm publish
This is the final step to getting that magic squirrel on npm. What this will do is read the package.json in the current folder and upload all the files/folders in there to the npm registry.
That’s it, you’re done, your magic squirrel is in the wild making the world a better place.
Some tips and tricks
- Have a README file and make sure you have a minimal documentation in there. Don’t let people figure out on their own how to use you package.
- Use .npmignore to choose what files and folders are published and installed. By default npm will use .gitignore, if available, but the files you want to publish may be different to those you want to ignore in git.
- Make sure your library works before publishing. You can make a test project and installing your package from a local folder (npm install ../path/to/your/project) and trying it out.
Final words
So, as you can see, it’s not that hard to have your own package published to npm. I hope you will start writting open source libraries and publish them. Have fun coding!
P.S: Questions? Want to have a chat? Send me an email at neculaesi.eduard@gmail.com and tell me more.