A library
or a package
is simply a collection of functions that someone has written and has made avaiable to the public either for free (open source) or by selling it. They may even be written by you!
Examples of libraries are:
- jQuery - Helper functions for JavaScript in general
- Express - A library for building a http server to host a website or webservice (API)
- sequalize - A library that is used to allow a JavaScript application to directly manipuate an SQL databasse.
In JavaScript, we use npm
or Node Package Manager
to manage the installation of packages for use in our Node JavaScript apps.
When we have a Node app, you will have a special file named package.json
that tells the app of all the currently installed packages.
Let's create a brand new Node app. Open the terminal and type the following:
mkdir -p ~/Development/first_node_app
cd ~/Development/first_node_app
npm init -f
To install express in our Node app, type in:
npm install -S express
Breaking this down:
npm
= Use the Node Package Manager
install
= Download and install the package
-S
= Save this application to package.json and install it in the current directory (first_node_app)
express
= The name of the package we want to install
When using Node
you can import these libraries for your own use with the require
keyword.
Let's create our app entry point with touch
. The UNIX command to create a new file.
touch index.js
And open our directory in atom / vsode
atom .
= Atom
code .
= VSCode
Then click on index.js
and enter the following:
var express = require('express');
// we can now use express in our app!