A brief introduction to NPM & Node.js

NPM stands for “Node package manager”. Node.js on the other hand is a JavaScript runtime environment that is cross-platform and open source. Node.js allows websites to render beautiful server sided dynamic webpages prior to being sent to the user. The NPM platform is currently the largest package registry in the world.

If one of your starting computer languages was ruby, you could say NPM is kind of like ruby gems. It consists of various libraries of code which you can cherry pick to suit whatever project you’re working on. You may or may not have realized it but if you’ve been actively coding in javascript, you’ve definitely utilized NPM at some point, especially at Flatiron school. For example a lot of coding exercises on websites such as learn.co, freecodecamp, and codeacademy use various node modules. NPM also happens to contain some of the best javascript debugging tools around.

Strong libraries of code

If you want to start using these libraries on your own, check if you have node.js and NPM installed. Open your terminal and enter the following.

node -v
npm -v

If a version comes up for both of these, it means you have the basic essentials installed to start acquiring packages, otherwise visit https://nodejs.org/en/ download node.js and then https://www.npmjs.com/ to make an account (I believe installing node.js automatically installs NPM.

Now that you’re ready, let’s install a package from NPM. First cd to your project directory.

Note: (You don’t have to type ALL of these different kinds of npm installs, these are just examples of how to deal with types of files/urls etc)
Source: https://docs.npmjs.com/cli/install

cd /development/enter-project-name/ 
npm install (with no args, in package dir) 
npm install [<@scope>/]<name> npm install [<@scope>/]<name>@<tag> npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder> alias: npm i common options: [-P|--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]

You’re almost there, now go to https://www.npmjs.com and find a package you’re interested in.

I’m going to use a simple example, a node module iv’e been working with for my javascript lab assignments. It’s called JSON-server (you’ve probably heard of it). Use this to spoof a REST api for a JSON file.

https://github.com/typicode/json-server (Here’s the github repository)

npm install json-server --save-dev

Hopefully the readme for the repository is legible and not too wacky, for this particular repository the readme happens to be excellent. Now follow their instructions and implement it into your code.

// server.js
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(router)
server.listen(3000, () => {
console.log('JSON Server is running')
})
$ node server.js

And there you go. You just successfully utilized your first node package. Hope you enjoyed the read, happy coding.