Home > Articles > Programming

This chapter is from the book

Package Management

For those who have a .NET background, package management should already be familiar. If you are comfortable with the concept of package management, feel free to skip the next paragraph, although you might learn a thing or two even if you are already comfortable.

There is a saying that if you don’t manage your packages, your packages will manage you. (In my experience, this is certainly true.) In the extremely innovative front-end community, libraries are constantly being updated, and many of these libraries rely on other libraries, which in turn rely on other libraries. This can cause a mess of dependencies that either forces you to spend hours resolving these dependencies or ultimately forces you to give up. This is where package managers come in. They maintain a list of libraries and their dependencies, and will help you automatically resolve these dependencies.

The first package manager discussed here is NPM.

NPM

The first thing you should know about NPM (Node Package Manager) is that’s automatically installed when you install Node. So, take the next 10 seconds to celebrate not having to install anything for this topic.

NPM is by far one of the most common tools for web developers. Where Node allows developers to build great development tools, NPM allows the developers to share their libraries, while simultaneously making it easy for people to install and update. NPM is by far the largest package manager for JavaScript libraries, boasting more than 125,000 packages at the time of this writing, with over a billion installs (that’s right, a billion, with a B) per month.

NPM can be accessed via command line. So, if you are on a Windows PC, open the command prompt. If you’re running OS X or Linux, open Terminal and type npm.

If you are experiencing errors, see the information about Node earlier (what it is and how to install it.).

Now that you have NPM up and running, let’s install a package.

If you closed the command prompt or Terminal, open it again and run the following command:

npm install -g bower

Let’s break this command down so that you better understand what it’s doing. By typing npm install, you are invoking NPM and telling it to install a package called Bower. The -g flag is telling Node to install this package globally. The reason you install Bower globally is that it is not project dependent.

If you receive an error message when trying to install Bower, you may need to run the command with elevated privileges. On a Windows computer, try running the command prompt as an administrator. If you are on OS X or Linux, try running the command with the sudo modifier:

sudo npm install -g bower

That should prompt you for your password, and even though it doesn’t look like you are typing anything, you are.

Follow these tips if you run into any problems installing packages throughout the remainder of this hour.

Bower

Bower is a package manager. Wait, didn’t you just learn that was what NPM is? That’s right! At this point, you might be thinking that the web development community just likes to complicate things (or that somebody is pulling your leg), and you would be partially right.

Bower is indeed another package manager, but one that focuses on installing front-end frameworks, libraries, and assets. While NPM can indeed install all of these things, it does so in a much different way. The following sidebar covers the differences between NPM and Bower.

To get started with Bower, create a new folder somewhere easily accessible, and navigate to it using your operating system’s terminal. Bower works just like NPM, where all you have to do is type bower install, but it can also grab files from other places. Here are some examples of ways to install libraries, assets, or even git repositories.

#### registered package
$ bower install jquery
#### GitHub shorthand
$ bower install desandro/masonry
#### Git endpoint
$ bower install git://github.com/user/package.git
#### URL
$ bower install http://example.com/script.js

Bower packages are installed in a subfolder called bower_components inside whatever folder you are currently in when you run the command.

Grunt

Grunt, at its core, is a task runner. Add a few of the hundreds of community-made plug-ins and you can automate your entire build process with just a few lines of code. Grunt uses Node’s require() system, along with adding some things to the package.json file. So, make sure to install Grunt as follows:

sudo npm install -g grunt-cli

This installs the Grunt command-line interface (CLI) globally so that it’s available to use in any of your projects. After you have it installed, you can start using it in your projects. To set it up, first make sure that you have a package.json file in your project directory. If you don’t, you can easily create one by answering a few questions after running the following command:

npm init

After answering questions from Node, you need to generate a Gruntfile, which is where you can set up tasks. To do that, run the following command:

npm install grunt --save-dev

The --save-dev flag tells the Grunt CLI that you want to make Grunt a development dependency.

This allows your fellow teammates, with just a single command, to make sure that they have all the necessary Node packages installed:

npm install

To add other premade Grunt tasks to your project, simply install them the same way you did Grunt. Here is the command to install JSHint, which is a code-validation task that ensures you write both valid and good JavaScript code:

npm install grunt-contrib-jshint --save-dev

Now that you have JSHint installed, let’s take a look at your Gruntfile. It should look something like this:

module.exports = function(grunt) {

  grunt.initConfig({
  });

};

To configure JSHint and set up a task to run it, modify your Gruntfile like so:

module.exports = function(grunt) {

  grunt.initConfig({
    jshint: {
      files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
      options: {
        globals: {
          jQuery: true
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');

  grunt.registerTask('default', ['jshint']);

};

This sets some options for JSHint, like what files it should examine and what globals you will have defined. Grunt then needs to make sure it loads the tasks, and finally, you register a task called default that will run JSHint. To actually run JSHint, just run the following:

grunt

This executes the default task that you just set up to run JSHint! While your IDE of choice may come with some built-in tools for front-end development, thousands of plug-ins can be used in any combination, providing you with the flexibility to automate your entire build. Grunt is an amazingly powerful task runner that can save you loads of time with your build and deployment process.

Yeoman

After reading through this hour, you might be thinking that it takes a lot of work to get all these tools set up for every new project you work on, and you wouldn’t be wrong. This is where Yeoman comes in. Yeoman is a scaffolding tool that allows anyone to write plug-ins for it. It’s simple to use and infinitely extensible. If you find yourself creating the same config files, installing the same packages, and scaffolding out the same folder structure every time you start a new project, you’re going to love Yeoman. If this is your first foray into JavaScript programming, you could probably use a little jumpstart. Let’s use Yeoman to scaffold out a brand new Angular application. To do so, let’s first install Yeoman, as follows:

npm install -g yo

Just like when you use -g in the Grunt install command, this ensures that Yeoman is installed globally so that you can use it wherever you are.

Let’s also run the following command to install the Yeoman Angular generator globally:

npm install -g generator-angular

At this point, make a new project folder and cd into it from your command prompt. Then run the following command:

yo angular MySweetNewAppName

Yeoman is going to ask you a few questions to make sure that it installs all the features you want. Once you complete all the prompts, Yeoman installs all the dependencies it needs and scaffolds a simple Angular application for you. To view what’s created, run the following Grunt command:

grunt serve

This command starts the Angular application within a locally running Node server where you can play with the app and debug it.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020