Raymond Daugherty
#100-Days=Of-Code

#100DaysOfCode – Day 1: JSON

Day 1 …

I know I’m not supposed to move too fast, but I’m going to do my best to stick to doing what I can, when I can. It may be over an hour a day, or (sadly) may be under an hour. My goal with Day 1 is just to start, so here goes…

Today with be a day on managing NPM packages. NPM (or Node Package Manager) is a way for developers to include packages, supplied by other developers, into their projects. These packages can have a variety of applications, from simply formatting time to much larger implications. Since I’m going to be using FreeCodeCamp for this process, I’ll do my best to link what I can, when I can.

One thing to note: In order to use NPM, you need to have NodeJS and NPM installed. If you don’t want to install NodeJS and NPM, you can use Glitch for the testing for FreeCodeCamp. Hopefully I’ll be able to do a post about installing both NodeJS and NPM in the future, but for now, Google and Glitch are your friends. 🙂

Node Modules

When starting a new project, NPM creates a folder that it saves all of the modules (packages) in, called “node_modules”. It also creates a JSON file, which allows you to define what modules you’d like to use in your project. You could include all of the available modules for every project into this folder, but that would make the overall directory cluttered and very large. That’s what I plan on keeping each project separate, and only install the necessary modules for that project.

Package.json, the Core of Any Node.js Project or npm Package

So, I took the above “heading” right from FeeCodeCamp. As much as I tried to find a clever way of rewording it, they actually summed it up in the best possible way. Without the package.json file, no NodeJS or NPM package would work, and the file is actually fairly simple to understand.

In a nutshell, you can define different values through what’s called a key:value pair. Using this notation, you can define the author of the project –

 "author":"Raymond Daugherty",

a description –

 "description": "This is the beginning process for learning NPM packages with FreeCodeCamp.com.",

even a version number for your project –

 "version": "1.0.0",

One of the things to pay attention to here is the use of commas. You have to make sure that all comma separated elements are followed appropriately with a trailing comma.

The important part of the package.json file is the “dependencies” section.

In the dependencies section, you’ll define the dependencies in an array format. You can think of this as a “key:value” pair that houses other “key:value” pairs. For example:

 "dependencies": {
"moment": "2.14.0"
}

The above code tells your NodeJS application to include the Moment package when the app is complied, and use the version 2.14.0. Versioning is important here, since on the slight change a newer version breaks your app, you can always move to a previous build.

When it comes to versioning, it’s best to remember this rule:

"package":"MAJOR.MINOR.PATCH",

The “MAJOR” is considered a change that may affect previous versions of the package. Going from version 2 to version 3 is considered a major upgrade, and can break things. (I’m lookin’ at you jQuery….)

The “MINOR” are simple updates that shouldn’t break the previous installations.

The “PATCHES” are simply bug fixes that needed to be made without groundbreaking changes to the code.

Keeping packages updated

In order to keep packages up-to-date, NPM has some special characters you can use. By using the tilde (“~”) in front of your package number, this allows NPM to update the package to it’s most recent bug fixes.

 "dependencies": { "moment": "~2.14.0" }

This doesn’t cause the package to upgrade to the latest version, but if any bugs get squashed by the developer, this will help apply the fix in the background.

Using the carot (“^”) in front of the version number allows NPM to update the package to the latest MINOR release. This will update the package but shouldn’t cause any issues with previous installations.

 "dependencies": { "moment": "^2.14.0" }

Finally, you simply remove packages by removing their call from the package.json file. Keep in mind, doing this may also break your app if your calling that dependency somewhere, so always be on the look-out.

That’s it for Day 1. Only 99 more to go.