Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4993

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T05:07:08+00:00 2024-11-27T05:07:08+00:00

Why I Switched To PNPM?

  • 61k

Package Manager For Node JS

It is system for that automate the process of adding/upgrading/removing and managing dependencies for a Node JS project

There are many package managers for Node JS

  • NPM (Node Package Manager)

Most popular package manager for Javascript and also default package manager for Node JS.

  • Yarn

It was released by Facebook Inc. in 2016. It was create to overcome problems and performance that were in NPM at that time.

  • PNPM

It is alternative package manager for Node Js for replacement of NPM, but faster and efficient.


Why is better than Yarn and NPM?

Imagine you install a package, Let call it Package_X. Imagine Lodash is one of dependency of Package_X. Now you install diffrent package call it Package_Y which also has Lodash as it dependency. Thus in a single project there two copies of Lodash.

If there are 100 packages using Lodash, you are going to have 100 copies Lodash

PNPM allows yoo to save tons of space

It is faster than both npm and yarn. Because Yarn copies files from cache wheareas pnpm links files into global score.

How PNPM works?

Note that PNPM doesn't flatten the dependency tree

Look how earlier node_modules tree looked like

node_modules/ |  Package_X/ |  | > node_modules/ |  |   | > Package_Z/ |  |       | index.js |  |       | package.json |  |   index.js |  |   package.json | |  Package_Y/ |  | > node_modules/ |  |   | > Package_Z/ |  |       | index.js |  |       | package.json |  |   index.js |  |   package.json 
Enter fullscreen mode Exit fullscreen mode

This way to managing has some issuse

  • Deeply nested dependency tree, causing long directory names in system.

  • Packages are copied and pasted several times when they are required in different dependencies.

But now after update in NPM @version 3, they added flattening so structure look like

node_modules/ |  Package_X/ |  |   index.js |  |   package.json | |  Package_Y/ |  |   index.js |  |   package.json | |  Package_Z/ |  |   index.js |  |   package.json 
Enter fullscreen mode Exit fullscreen mode

But pnpm follow diffrent apporch rather than flattening tree it keeps it same

In /node_modules folder created by pnpm, each package has it's own dependency but dependency tree is never deep as in earlier version of npm. It keeps all dependencies flat with use of Symbolic Links or Junction (in windows)

node_modules/ |  Package_X/ |  | > node_modules/ |  |   | > Package_Z/ -> ../../../Package_Z/1.0.0 |  |   index.js |  |   package.json | |  Package_Y/ |  | > node_modules/ |  |   | > Package_Z/ -> ../../../Package_Z/1.0.0 |  |       | index.js |  |       | package.json |  |   index.js |  |   package.json | |  Package_Z/ |  |   index.js |  |   package.json 
Enter fullscreen mode Exit fullscreen mode

Installation

  • Open terminal
  • Execute following command
  npm install -g pnpm 
Enter fullscreen mode Exit fullscreen mode

or

  npx pnpm add -g pnpm 
Enter fullscreen mode Exit fullscreen mode

Small Project Using pnpm

We are going to build Restfull API that get name of two person and calculate the love percentage between them

Exexute below commands

Create a directory

  mkdir love-api 
Enter fullscreen mode Exit fullscreen mode

Initialise it as pnpm project

  pnpm init -y 
Enter fullscreen mode Exit fullscreen mode

We are going to use Express for it.
Note pnpm commands is quite similiar to both npm and yarn. We are going to replace npm install [PACKAGE_NAME] with pnpm add [PACKAGE_NAME]

So adding following packages to your projects

Execute below commands

  pnpm add express cors 
Enter fullscreen mode Exit fullscreen mode

  pnpm add -D @types/express @types/cors nodemon typescript concurrently 
Enter fullscreen mode Exit fullscreen mode

Add these below script to package.json

{   "build": "tsc",   "start": "node dist/index.js",   "dev": "concurrently "tsc -w" "nodemon dist/index.js"" } 
Enter fullscreen mode Exit fullscreen mode

We are going to discuss only about the PNPM side of things only
have look of source code here.

Dont Forget To Follow Me -> Harsh Rastogi

Now to build convert typescript code into javascript

In npm we do npm run build but in pnpm we have to execute

  pnpm build 
Enter fullscreen mode Exit fullscreen mode

and to start dev server

  pnpm dev 
Enter fullscreen mode Exit fullscreen mode

and to start server in production mode

  pnpm start 
Enter fullscreen mode Exit fullscreen mode

Benchmarks

Benchmark compares the performance of NPM, Yarn, PNPM

Image description

Conclusion

If you are searching that gives you better speed and performance then pnpm is better, I personally suggest you to use pnpm instead of npm and Yarn. If you are not using it then you a chance to try it.

Yarn sends date to Facebook, which don't make yarn suitable in some scenarios. NPM has also security issue that's why there is Yarn now.

In above benchmarks we can see PNPM is better in all aspects.

Happy Coding 🙂

javascriptnodepnpmwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.