npm tips and tricks

In my previous post I showed you how easily you can create a simple webserver using Node.js. In this post I want to show you how to make more advanced usage of node package manager.

npm init

Using node package manager you can get an even quicker start of your project by using the npm init command. So let’s get started by opening a command prompt (on windows open the Node.js command prompt). Then create a new folder and navigate into this newly created folder. In the folder execute following command and answer the questions or press enter for the defaults.

Your environment has been set up for using Node.js 0.10.17 (x64) and npm.Press any key to continue . . .C:\Users\Marco> mkdir NodeJsPackageExampleC:\Users\Marco> cd NodeJsPackageExampleC:\Users\Marco\NodeJsPackageExample> npm initThis utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sane defaults.See `npm help json` for definitive documentation on these fieldsand exactly what they do.Use `npm install --save` afterwards to install a package andsave it as a dependency in the package.json file.Press ^C at any time to quit.name: (NodeJsPackageExample) node-js-package-exampleversion: (0.0.0) 0.0.1description: Total package awesomenessentry point: (index.js)testcommand:git repository:keywords: package, awesomenessauthor: Marco Franssenlicense: (BSD-2-Clause) MITAbout to write to C:\Users\Marco\NodeJsPackageExample\package.json:{"name": "node-js-package-example","version": "0.0.1","description": "Total package awesomeness","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" &amp;&amp; exit 1" },"keywords": ["package","awesomeness" ],"author": "Marco Franssen","license": "MIT"}Is this ok? (yes) yes

After answering the questions the result should be something like above. As you can see I have changed the default name. By default node package manager uses the folder name. Play around yourself to get the desired result. You can also change the package.json after it is created.

Why do we need this package.json file?

The answer is simple… When we put our code in source control (GIT, SVN, TFS) we don’t want to commit all our third party packages. We want to leave them out of the source control. Another reason is we also don’t want to ship all dependencies as part of our own package when we release our package to the npm registry. All team members or users of your package who checkout your code or download your package are simply missing all the dependency packages and this is where the packages.json comes in. After downloading the package, they simply execute npm install._ This_ will download all dependencies etc. based on your package.json file.__

npm install

So let’s continue with some more npm tips and tricks. The second thing you may already have seen by executing npm init is the npm install –save command. Npm install –save will install your package and saves it in the package.json file. Skipping the –save option will simply not add the package to your package.json file. Let’s start by adding the express package, which we also used in previous post.

Now you know how to install a package and save it to the package.json. To uninstall a package and remove it from your package file just execute the npm uninstall –save command.

repository and readme.md

When you just like me skipped the repository during npm init, you just have seen two warnings. Let’s solve both by adding a repository in the package.json file and a Readme.md file in the root of our package.

Do not forget to add some contents in your Readme.md file, otherwise npm won’t be satisfied. A title and a short description should be ok.

npm install dev package

When deploying a package on a server you don’t want your development packages published on the server. An example of such a development package is grunt. So we want to save such package in a different way so our colleague developers can install the development packages, but our production server can skip them.

As you can see all dependencies of grunt are automatically installed and a new property is added to your packages.json file.

123

"devDependencies": { "grunt": "~0.4.2"}

With grunt installed you can speed up your development even more by automating a lot of tasks. These are tasks like running tests, minifying, compiling Less/Sass etc. I would like to challenge you and share your grunt scripts with me and the rest of the developer community by posting a reaction on this post. For more help on _grunt_ visit http://gruntjs.com/.

private package

If your package is not intended to be shared or reused on the _npm registry _make sure to protect yourself by adding the following setting in your package.json.

"private": true

This will prevent you from accidentally publish your package to the npm registry.

npm update

While working on your awesome package you probably want to update your dependencies before you publish your package so you are running the latest code of all packages.

To check if there are any package updates available you can run the following command.

Node package manager uses semantic versioning and it is up to the package creator/publisher to follow these guidelines. So always double check what has changed and see if your package still works after upgrading the dependencies. Usually you should be safe when installing patch releases. Be more carefull when you upgrade minor or major releases. More information about versioning can be found here. Make sure your package satisfies these guidelines when publishing new and updated packages since this will help other developers during the update process.

npm publish

Creating a package is one thing, but the second thing is you probably want to share it with the community. Sharing a package with the community is pretty straight forward.

First of all we need to set our npm author info if you haven’t already done so. This can be done by executing following commands.

unpublish

Since the package I just published won’t be of any value I removed it using following command.

12345678

C:\Users\Marco\NodeJsPackageExample> npm unpublish ./ --forcenpm WARN using --force I sure hope you know what you are doing.......npm http DELETE https://registry.npmjs.org/node-js-package-example/-rev/3-366120337d9f89cd2ca7fc0c4489281d......

I hope this article showed you some of the nice stuff you can do to quickly build your first node package and get it published in the npm registry. Using npm you should get better control over your dependencies and it should become easier for you and your team to keep track on those dependencies.