02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is Node.js.

Version Controlling Packages in NPM

Version Controlling Packages in NPM

In this tutorial, we will cover how to version control packages in NPM (Node Package Manager). Managing package versions effectively is crucial for keeping your projects stable and ensuring compatibility across different environments. This tutorial was originally created in 2016, and we’ve added important updates to make sure you’re working with the latest tools and best practices.

Step 1: Understanding Versioning in NPM

NPM uses semantic versioning (also known as semver) to manage package versions. Understanding the format and how versioning works is key to managing your dependencies effectively.

Semantic Versioning Format

The version format follows MAJOR.MINOR.PATCH, for example: 1.2.3.

  • MAJOR: Changes when incompatible API changes are made.
  • MINOR: Changes when functionality is added in a backwards-compatible manner.
  • PATCH: Changes when backwards-compatible bug fixes are made.

Example:

  • 1.0.0: Initial release.
  • 1.1.0: New feature added.
  • 1.1.1: Bug fixed.

Step 2: Specifying Versions in package.json

When you add dependencies to your project, you can specify the version using package.json.

  • Exact Version: To use an exact version, specify it like this in package.json:

    "express": "1.0.0"
    
  • Compatible with Minor/Patch Updates: Use the caret (^) symbol to allow updates that do not break compatibility:

    "express": "^1.0.0"
    

    This allows any version that is greater than or equal to 1.0.0, but less than 2.0.0.

  • Patch Updates Only: Use the tilde (~) symbol to allow patch-level changes:

    "express": "~1.0.0"
    

    This allows any version greater than or equal to 1.0.0, but less than 1.1.0.

Critical Update (2024): Using lock files like package-lock.json is essential for ensuring consistent installations across environments, especially for production deployments. Lock files ensure all team members use the same versions, preventing unexpected behavior.

Step 3: Updating Packages Safely

To keep your project up to date with the latest bug fixes and features, it's important to regularly update your dependencies.

Checking for Outdated Packages

To check for outdated packages, run the following command:

npm outdated

This command will show a list of all outdated packages, including the current version, the wanted version, and the latest available version.

Updating Dependencies

  • Minor and Patch Updates: You can update a package to the latest minor or patch version by using the npm update command:

    npm update package-name
    
  • Major Updates: For major updates, use npm install with the version number to avoid breaking changes:

    npm install package-name@latest
    

Tip: Always read the release notes of major updates to understand potential breaking changes before updating.

Step 4: Managing Dependencies with package-lock.json

The package-lock.json file is automatically generated when you install packages. It captures the exact version installed, ensuring consistency across different environments.

  • Commit package-lock.json: It is a best practice to commit the package-lock.json file to your version control system to ensure that everyone on your team has the exact same dependency versions.

Step 5: Removing Old Versions of Packages

To remove a package from your project, you can use the npm uninstall command:

npm uninstall package-name

This command will remove the package from your node_modules folder and also delete the entry from package.json.

Conclusion

Version controlling packages in NPM is an essential skill for managing dependencies and ensuring stability in your projects. Understanding semantic versioning, using package-lock.json, and updating dependencies appropriately can save you from potential issues and keep your development workflow smooth.

By following the steps in this tutorial, you can manage your project’s dependencies effectively, keeping your codebase up to date while minimizing compatibility issues. Always make sure to stay informed about new updates, especially for major changes, and leverage lock files for consistent package installations.

Video Transcript

00:00Uh, welcome back.
00:03It's time for us to wrap up our debate conversation,
00:07learning experience about NPM,
00:09and we're going to capture really one
00:11of the most critical things you need to know,
00:13which is version controlling.
00:15In this lecture, you're gonna learn
00:16and understand the VER rules, which are rules that govern
00:21how do you define different updates in your application?
00:24And for that, how do you know
00:25what changed in different applications that you're using
00:29With it, we're gonna learn about the three main ways
00:31that you declare to the world of an update through a patch,
00:35a minor update, or a major update.
00:39But the most important part of this is
00:40that you're gonna understand better how to work
00:43with your own packages and other people packages.
00:46So to make sure, for example, if you have a site
00:49or application and you're depending on other people's codes,
00:53you wanna make sure that you're not gonna get any updates
00:56that would break your functionality,
00:58but you still want to get updates that fix bugs.
01:01You still want to get updates that address issues
01:04that would not break your functionality.
01:06And we're gonna talk a lot about that in this lecture.
01:08So by the end of this lecture,
01:09you're gonna have a full grasp
01:11and understanding of how to work with Var SemVer.
01:14Alright, so let's jump right into it.
01:18To wrap up our conversation about NPM
01:21and really empowering you with all the things you need
01:24to know to be really great at full Stack development is you
01:28need to understand how version and control works.
01:32Now, I want to focus in our, on our package,
01:34do json file on our version that currently
01:37by default is 1.0 0.0.
01:41Now, the NPM goes through a convention
01:45that's called SemVer,
01:47and I've added into the links to this chapter,
01:49this full specification of ver We're not gonna go
01:53through the full specification,
01:54but I want you to understand how it works.
01:56It's vitally critical.
01:58Now, the first digit between the three, it's always numbers.
02:01This is the first, first rule of thumb. It's always numbers.
02:04There are three different numbers, always
02:06with a.in between each.
02:08The first number represents the major release version,
02:11where zero would mean this is not stable enough
02:15for the public to consume you.
02:17Once you move to version one, that means it's stable
02:20and ready for people to consume beyond yourself, um,
02:24or your alpha testers and so forth.
02:27The next line defines main minor versions,
02:31and the last one is patch.
02:33Now, what does these all mean?
02:35Patch means is that you're making a fix
02:38that does not break functionality or add new features.
02:41All you're doing is fixing bugs
02:43and making sure that things are working.
02:45And then you would increase your patch version.
02:48Every increase in the patch version is linear,
02:51one after each other.
02:53Now, you would never make an update
02:55and keep the same version.
02:57Every time you make a patch
02:58and you're releasing it to the public, you need
03:00to upgrade the version number of your patch.
03:03So even if it's a small bug fix
03:05and you notice that your bug wasn't fully addressed,
03:08you still need to update the version if it
03:10was published to the world.
03:12The next is minor.
03:13Minor is when you're not breaking any functionality,
03:16but you're adding a new feature.
03:18Now, whenever you add a new feature without breaking
03:22functionality, you reset your patch number back to zero
03:25and you incre increase the minor version by one.
03:31Last but not least is when you're creating new functionality
03:34that breaks previous versions.
03:37And in that case, you're gonna push up to a new version,
03:40or in the case where you're actually publishing
03:42for the first time, your first version
03:44that is publicly accessible each time you will then reset
03:48all the ones that are underneath.
03:50Now again, it's only numbers, no leading zeros.
03:54You wouldn't put a leading zero.
03:55It's only numbers, no letters
03:57or anything of that, anything like that.
04:01Once a version is released,
04:02you cannot make more modifications to that version no matter
04:05what, but you have to increase the version.
04:07If it's a patch, then make a patch update.
04:10Um, one means public, as we were saying,
04:13and you each time reset the lower tiers.
04:16Whenever you make an update.
04:17I'm gonna go ahead and set our version to be zero zero,
04:20and I wanna show you how to make these updates, how
04:23to create these updates directly on terminal.
04:26And I'm gonna go into my terminal
04:28and I'm gonna type NPM version patch.
04:32When I do a version patch,
04:33it'll basically take whatever version we're in
04:36and add a new patch.
04:37Again, we'd only do that
04:39whenever we're creating a fixed,
04:40uh, a bug that we've created.
04:42So we would increase the patch each time.
04:44When we do that, whenever we're gonna be adding
04:48functionality that is not breaking current functionality
04:51will make a minor update.
04:53Whenever we're making a minor update,
04:55it's gonna bring up the value within the middle, um, digit
04:59and drop back to zero our patches
05:02unless we have another patch that we need to make.
05:04And then we're gonna be in that minor version one
05:07and patch version one.
05:08And again, this automatically updates our JSON file,
05:12our package JSON file, which is the descriptor of our app.
05:17Now, beyond that, let's continue
05:18and also do the last one, which is a major update.
05:21And when we do a major update, it's gonna push up
05:23to a new version one and reset all the values underneath.
05:28Now I want to talk just about two more things.
05:31Whenever we're working with dependencies, they have a,
05:34a cer uh, they have that version in control as well.
05:38And notice that here we have is array as a dependency
05:41and we're requesting version number one.
05:44But what we're really asking for is we're saying
05:47that we only want minor updates.
05:50I wanna go through and show you how this logic works.
05:53Whenever we have a version, for example,
05:55we just installed version one
05:57and we wanna make sure
05:59that we could install all the different patches,
06:02but only patches, no new features.
06:04In that case, we could set it to be one zero
06:06and an x, where a number would've been,
06:09or set a, a round, I don't even know how to call this.
06:13A, a wave. We'll put a wave in the beginning
06:15of the version saying that we only want patch updates,
06:19no minor or major updates in a case
06:23where we wanna get everything, well,
06:25when we wanna get minor updates, we could
06:28or set the version to be with the X right here in the,
06:31in the area where the minor would've been.
06:34Or instead of that set here, uh, uh, uh, um, a roof I think
06:39that was be called that will basically say that we'll owe
06:42to accept minor updates.
06:43If you don't set anything,
06:45that will automatically take everything that is possible,
06:48that means if, even if it'll break functionality,
06:50which is something that we would never want.
06:53Now in general, by default, it's gonna always get the one,
06:56the minor updates,
06:58but no major updates in a situation
07:00where you wanna specifically set
07:02and say, I want to, NPM install, I wanna install is array
07:07and I want to save it, right?
07:09But I also, not only do I want save it, i I want
07:12to actually go to my RA and I'm gonna put here, uh, at,
07:15and I'm gonna define what version I want.
07:18I want version one point x, meaning
07:20that I want all feature updates,
07:22or maybe I just want it to be just patch updates.
07:25And then I'm gonna go ahead and save it
07:27because I want it
07:28to be saved into my js o my package at JSON file.
07:32Once I get through the process
07:34and I install the package, if we go back into our descriptor
07:38and we reload it, we're gonna see that we're gonna get
07:40that only with the patch updates,
07:42with the minor updates in this case.
07:44Really, it's not the, it, it did not refresh
07:46or did it refresh?
07:48Let's go, let's, let's uninstall our package
07:50and save it for a second.
07:52So let me just go back in here
07:53and instead of that, I'm gonna remove
07:58and I'm gonna remove our Isra.
08:05Okay, so once I've removed the Isra, if I go back,
08:07we'll see we don't have any dependencies
08:10and I'm gonna go ahead and install it one more time.
08:13And this time in my isra I'm going to, I'm gonna specify
08:16that I want to have version one point X
08:22And it's gonna install the package.
08:24And if I return back into my package, we'll see
08:27that now we have that, uh, that uh, uh, that, um,
08:33I forget the name of it, that, um, what we call it, roof.
08:36We're gonna have that roof that specifies that.
08:39We want OTO minor updates and not only patches.
08:43So there we go. That's it.
08:44That's all you really need to go know about SemVer.
08:47And in general, I encourage you, even if you're not planning
08:49to publish this specific package,
08:51'cause we're just starting our development,
08:53if you wanna learn more about publishing,
08:55go into the documentation
08:56and NPM to learn, how could you publish your own work
08:59that we're gonna be creating in node js in the
09:01following chapter.
09:03But until then, make sure
09:04that you're working with the SemVer.
09:06Make sure that you're working with the NPM version,
09:08minor patch major whenever you're making updates.
09:11So that's it, that's it for this chapter
09:13and I hope you feel very comfortable.
09:15Now with NPM in this lecture,
09:18we really explore deeply the world of EM
09:21and whether we completed this section.
09:23In the next section, we're gonna dive deep into the world
09:26of node js.
09:29I.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!