Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Full-Stack Web Development with Vue.js and Node

You're reading from   Full-Stack Web Development with Vue.js and Node Build scalable and powerful web apps with modern web stack: MongoDB, Vue, Node.js, and Express

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781788831147
Length 366 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Aneeta Sharma Aneeta Sharma
Author Profile Icon Aneeta Sharma
Aneeta Sharma
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Introducing MEVN FREE CHAPTER 2. Building an Express Application 3. Introducing MongoDB 4. Introducing REST APIs 5. Building the Real Application 6. Building Authentication with passport.js 7. Building OAuth Strategies with passport.js 8. Introducing Vuex 9. Testing an MEVN Application 10. Going Live 11. Other Books You May Enjoy

Introducing npm

The npm is the acronym for Node Package Manager. Basically, it is a tool that takes care of all the packages that we install for Node.js. We can find all the existing packages on the official website (https://www.npmjs.com/). npm makes it easy for developers to keep their code updated and to reuse code shared by many other developers.

Developers are often confused by the terms package and modules. However, there is a clear distinction between these two.

Module

A module is something that can be loaded by Node.js with a require command and has a namespace. A module has a package.json file associated with it. 

Package

package is just a file, or group of files, that is capable of functioning on its own. Every package also has a package.json file that contains all the metadata-related information that describes that package. A combination of modules makes up a node package.

Installing npm

When we install Node.js from the installer itself, npm is installed as a part of the node. We can check whether npm is installed or not by using the following command:

$ npm --version

If npm is not installed, the command displays an error, whereas if installed, it just prints out the version of the installed npm.

Using npm

npm is used to install different packages in our application. There are two ways to install packages: locally and globally. When we want to install a certain package specific to our application, we want to install that package locally. However, if we want to use a certain package as a command-line tool or be able to access it outside our application as well, we will want to install it as a global package.

Installing an npm package locally

To install a package specific to our application only, we can use this command:

$ npm install <package_name> --save

Installing an npm package globally

To install a package globally, we can use this command:

 $ npm install -g <package_name>

Introducing package.json

All the node packages and modules consist of a file called package.json. The main function of this file is to carry all the meta information associated with that package or module. A package.json file requires the content to be a JSON object.

As a minimum, a package.json file consists of the following things:

  • name: The name of the package. This is an important part of a package.json file as it is the main thing that distinguishes it from other packages and, hence, it is a required field.
  • version: The version of the package. This is also a required field. To be able to install our package, the name and version fields need to be given.
  • description: A short summary of the package.
  • main: This is the primary entry point used to look for the package. Basically, it is a file path, so when a user installs this package, it knows where to start looking for the modules.
  • scripts: This field consists of commands that can be run for various states in the application. It has a key-value pair. The key is the event at which the command should be run and the value is the actual command.
  • author/contributors: The author and contributors are the people. It contains an identifier of the person. An author is a single person, whereas contributors can be a group of people.
  • license: The license field, when provided, makes it easy for the users to use our package. This helps in identifying the permissions and restrictions when using the package.

Creating a package.json file

We can manually create a package.json file and specify the options ourselves, or we can use a command to create it interactively from the command prompt. 

Let's go ahead and initialize a sample application with a package.json using npm.

First, create a folder in your projects directory using the command:

$ mkdir testproject

To create a package.json file, run the following command in the application that we created:

$ npm init

Running this command will ask us a bunch of questions that we can answer interactively from the command line:

In the end, it will create a package.json file, which will have the following content:

You have been reading a chapter from
Full-Stack Web Development with Vue.js and Node
Published in: May 2018
Publisher: Packt
ISBN-13: 9781788831147
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image