Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Difference between tilde (~) and caret (^) in package.json

    Will ButtonWill Button

    As a beginning node.js user, you will often see the tilde (~) or caret (^) in front of the version number for dependencies managed by your package.json file. In this lesson, you will learn what each means, when to use it, the implications of each and a brief introduction to Semantic Versioning.

    javascriptJavaScript
    nodeNode.js
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:01 If you look in the dependencies in your package.json file, you'll see all of the dependencies listed like this. We have the package name followed by the version number.

    00:10 But frequently, you'll also see the tilde or the carat prefacing the version specified. The tilde or the carat aren't required. You can specify your version like this and that will satisfy the dependency for your application.

    00:25 This is also going to lock in the exact version of the dependency, meaning that for connect-mongo only version, 1.1.0is going to be installed, regardless of what the latest version is. If instead I specify a tilde in front of it, it's going to lock the dependency to the specified minor version.

    00:46 This means that if the package dependency updates to version 1.1.1, your node modules directory will update to that version the next time you run npm install or npm update. That's going to remain true for all patch-level upgrades, if the latest release version increments to 1.1.2, we'll get it, or 1.1.3 or 1.1.4, and so on, but it will not upgrade to version 1.2.

    01:18 The carat, on the other hand, is used for locking the major version, meaning that all release versions in version 1 release cycle meet the dependency requirements. That's going to include version 1.1, version 1.2, version 1.3, and so on, but it will stop once the package version increments to version 2.0The remaining question becomes, "Who cares?" and the answer lies within semantic versioning. According to semantic versioning rules, the rightmost number is used for the patch level. This is primarily used for releasing bug fixes for the current version, and it doesn't break backwards compatibility.

    02:00 The middle number, known as the minor version, is used to indicate bug fixes and new features that, again, don't break backwards compatibility and should be safe to apply.

    02:10 The first number in semantic versioning indicates a major version and it indicates that there are breaking changes with previous versions, and so, it should be heavily tested before updating your application dependencies.

    02:24 The caveat here is these rules of semantic versioning rules only apply if the developer of the package you're depending on uses semantic versioning rules.

    Discuss

    Discuss