Skip to content

Latest commit

 

History

History
 
 

2-packages

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

2 - Installing and using a package

In this section we will install and use a package. A "package" is simply a piece of code that someone else wrote, and that you can use in your own code. It can be anything. Here, we're going to try a package that helps you manipulate colors for instance.

  • Install the community-made package called color by running yarn add color.

Open package.json to see how Yarn automatically added color in dependencies.

A node_modules folder has been created to store the package.

  • Add node_modules/ to your .gitignore file (and git init a new repo if you haven't done that yet).

You will also notice that a yarn.lock file got generated by Yarn. You should commit this file to your repository, as it will ensure that everyone in your team uses the same version of your packages. If you're sticking to NPM instead of Yarn, the equivalent of this file is the shrinkwrap.

  • Add const Color = require('color'); in index.js
  • Use the package like this for instance: const redHexa = Color({r: 255, g: 0, b: 0}).hexString();
  • Add console.log(redHexa).
  • Running yarn start should show #FF0000.

Congratulations, you installed and used a package!

color is just used in this section to teach you how to use a simple package. We won't need it anymore, so you can uninstall it:

  • Run yarn remove color

Note: There are 2 kinds of package dependencies, "dependencies" and "devDependencies". "dependencies" is more general than "devDependencies", which are packages that you only need during development, not production (typically, build-related packages, linters, etc). For "devDependencies", we will use yarn add --dev [package].

Next section: 3 - Setting up ES6 with Babel and Gulp

Back to the previous section or the table of contents.