Node.js | Section 9 | Node Package json | NodeJS Beginners | Simple Tutorials | Easy Learning

Опубликовано: 06 Май 2022
на канале: Tech Stack Learning
18
1

Hi All, Welcome back to NodeJS tutorial course. I hope you all have watched my previous sessions which explains different concepts of nodejs and live coding examples. If not, below are the links -
Node.js - Section 1 Introduction & Setup -    • Node.js | Section 1 | Introduction & Setup...  
Node.js - Section 2 Architecture & Features -    • Node.js | Section 2 | Architecture & Featu...  
Node.js - Section 3 V8 Engine & First Example -    • Node.js | Section 3 | V8 Engine & First Ex...  
Nodejs - Section 4 Globals & Live Examples -    • Nodejs | Section 4 | Globals & Live Exampl...  
Nodejs - Section 5 REPL Console -    • Nodejs | Section 5 | REPL Console | NodeJS...  
Node.js - Section 6 Nodejs Basics -    • Node.js | Section 6 | Nodejs Basics | Node...  
Node.js - Section 7 Modules -    • Node.js | Section 7 | Modules | NodeJS Beg...  
Node.js - Section 8 Node Package Manager -    • Node.js | Section 8 | Node Package Manager...  

Please do watch.

Today in this video i am going to explain you all about package.json and its features and usage. Will also be showing live example on how to use them.

Package.json---
If you work with JavaScript, or you've ever interacted with a JavaScript project, Node.js or a frontend project, you surely met the package.json file.
What's that for? What should you know about it, and what are some of the cool things you can do with it?

The package.json file is kind of a manifest for your project. It can do a lot of things, completely unrelated. It's a central repository of configuration for tools, for example. It's also where npm and yarn store the names and versions for all the installed packages.

Command-specific properties----
The package.json file can also host command-specific configuration,
for example for Babel, ESLint, and more.

Each has a specific property, like eslintConfig, babel and others.
Those are command-specific, and you can find how to use those in the respective command/project documentation.

package-lock.json----

In version 5, npm introduced the package-lock.json file.

What's that? You probably know about the package.json file,
which is much more common and has been around for much longer.

The goal of package-lock.json file is to keep track of the exact version
of every package that is installed so that a product is 100% reproducible
in the same way even if packages are updated by their maintainers.

This solves a very specific problem that package.json left unsolved.
In package.json you can set which versions you want to upgrade to (patch or minor), using the semver notation, for example:

if you write ~0.13.0, you want to only update patch releases: 0.13.1 is ok,
but 0.14.0 is not.
if you write ^0.13.0, you want to get updates that do not change the
leftmost non-zero number: 0.13.1, 0.13.2 and so on. If you write ^1.13.0,
you will get patch and minor releases: 1.13.1, 1.14.0 and so on up to 2.0.0
but not 2.0.0.
if you write 0.13.0, that is the exact version that will be used, always
You don't commit to Git your node_modules folder, which is generally huge, and when you try to replicate the project on another machine by using the npm install command, if you specified the ~ syntax and a patch release of a package has been released, that one is going to be installed. Same for ^ and
minor releases.

If you specify exact versions, like 0.13.0 in the example, you are not affected by this problem.

It could be you, or another person trying to initialize the project on the other side of the world by running npm install.

So your original project and the newly initialized project are actually different. Even if a patch or minor release should not introduce breaking changes, we all know bugs can (and so, they will) slide in.

The package-lock.json sets your currently installed version of each package in stone, and npm will use those exact versions when running npm ci.

This concept is not new, and other programming languages package managers (like Composer in PHP) have used a similar system for years.

The package-lock.json file needs to be committed to your Git repository,
so it can be fetched by other people, if the project is public or you have
collaborators, or if you use Git as a source for deployments.

The dependencies versions will be updated in the package-lock.json file when you run npm update.

NPX----
npx is a very powerful command that's been available in npm starting version 5.2, released in July 2017.

If you don't want to install npm, you can install npx as a standalone package

npx lets you run code built with Node.js and published through the npm registry.

Live examples are available in video