Skip to content

Instantly share code, notes, and snippets.

@robwilson1
Last active August 21, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwilson1/428ad01fbfe5f73a824da0d7388adc30 to your computer and use it in GitHub Desktop.
Save robwilson1/428ad01fbfe5f73a824da0d7388adc30 to your computer and use it in GitHub Desktop.
Libraries

Libraries and Node

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.

Installing a package

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

Requiring a package

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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment