Luke Warren Dev
Software Development Tips and Stories When I feel Like It

NPM for .NET Developers

NPM is essentially to JavaScript what Nuget is to .NET.

,
Categories: node
Tags: .NET , npm , node
NPM for .NET Developers

What is it?

N - Node
P - Package
M - Manager

NPM is the package manager that comes bundled with Node. It is the defacto package manager for JavaScript.

NPM is essentially to JavaScript what Nuget is to .NET.

Why should we use it? Isn't Nuget good enough?

No. Nuget might have some of the commonly used JavaScript libraries but Microsoft actually advises people use npm or similar for managing their client-side dependencies.

Besides from that, Nuget installs scripts all over your project making things messy and difficult to manage.

NPM is where 99% of all the JavaScript libraries live. If you find a library that is not on NPM please email me at luke@fake-email-address.com

How to get it?

Just go to the Node download page and install the MSI. This will install both Node and NPM along with it

https://nodejs.org/en/download/

Note - You cannot download NPM directly

How do I interact with NPM?

The best way is just via the command line. This might seem daunting at first but there are only a few commands to learn

What is the package.json file?

The package.json file is where NPM stores the dependency graph for the given folder.

It is not mandatory but is advised so that you do not have to source control your dependencies.

Think of this file as the equivalent to the packages.config that Nuget uses.

Creating a package.json file is super easy via the npm init command.

Example package.json:

{
  "name": "kill",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "liberty",
  "license": "ISC"
}


Where are the dependencies files stored?

All dependencies are stored in the node_modules folder

How do I install a package?

1. You will first want to create the package.json file (if you don't have one already).

Enter npm init into your command terminal and follow the prompts. Some things you can just leave blank and defaults will be used.

2. Find the package you want to install.

Most packages are pretty well named but it is worth checking on the NPM website to confirm the exact name of the package you wish to install.

Website: https://www.npmjs.com/

3. Install the package

Enter npm install <<name_of_package>> --save into your terminal to install the package.

For example, to install jQuery we would enter npm install jquery --save. This will install the latest version of jQuery.

Notes

  • --save indicates that you want to add the package to the package.json file.
  • install can be shortened to just i. Example: npm i jquery --save

How do I uninstall a package?

Uninstalling a package means 2 things:

  1. Remove the package and it's dependencies from node_modules
  2. Remove the entry from package.json

So to uninstall we simply enter npm uninstall <<name_of_package>> --save into our command window.

For example, npm uninstall jquery --save

I pulled down a project and the node_modules folder is missing!

Because all packages are stored in the package.json file we don't actually need to save the node_modules folder in source control.

Just enter npm install to restore all the packages for the given folder you are in.


comments powered by Disqus