PNPM: Because to work more flexible we start from the bottom
source link: https://medium.com/jobtome-engineering/pnpm-because-to-work-more-flexible-we-start-from-the-bottom-5a9c3a9c2af4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
PNPM: Because to work more flexible we start from the bottom
pnpm is a nodejs
package manager which has as its main objective to reduce the number of packages installed on your local machine and to speed up the installation of packages.
How does all this work?
To do this, pnpm
, uses two really simple concepts but are not so easy to implement:
- Sharing of the dependencies. All the dependencies will be installed in a unique store and linked (hard or soft, depending on the package) in your
node_modules
folder, this means that if you use the same package (and version) on 100 projects you will install it just once. - Light package updates. When you update a package, using the
pnpm update
command, only the files changed inside the package will be saved in the store, so you won’t have a redundant and complete folder for all your versions of the same package but a complete version and the diff of the new version.
In the end, pnpm
changes the structure inside the node_modules
folder to better manage symlinks and other magic stuff but all of this is transparent to you. You will be able to use your packages inside your code as always at the cost of using pnpm
instead of using npm
or yarn
How to install it?
To install pnpm
you have two main ways:
- Standalone: using
curl
,wget
,brew
, etc… - As an npm package, like yarn, using the
npm install -g pnpm
command
I advise you to install it as a standalone package, but it’s up to you, all the references on how to install it can be found at this link:
Installation | pnpm
On POSIX systems, you may install pnpm even if you don't have Node.js installed, using the following script: If you…
What changes from npm?
At the usage level, there are no differences, in addition to having to remember to use pnpm
instead of npm
(for me is very difficult because the names are really too similar, maybe could be a great idea to create a bash alias?), otherwise, the CLI is deliberately so similar. You will have the opportunity to:
- Install all the dependencies declared inside your
package.json
using thepnpm install
command - Add new dependencies using the
pnpm add <package>
command - Update all the dependencies to the newest version declared inside your
package.json
using thepnpm update
command - Checks for known security issues with the installed packages using the
pnpm audit
command
Why do we use it?
Speed and saving space on our local machines are not the only reasons why we use pnpm in our projects. There are two other reasons that I will go on to explain, one very tangible and another that is in the roadmap for our future.
Monorepo
In Jobtome we love monorepo
for a lot of reasons. The workspace management of pnpm
, which allows you to use internal repo packages as node_modules
, is really great. The interface is quite similar to the yarn interface but with a lot fewer limitations and issues. Moreover, the use and the management regarding lerna
are certainly 1000 times superior.
To set and use your workspace in pnpm
you have to:
- Create a
pnpm-workspace.yaml
file at the root of your project to define the packages:
- Referencing workspace packages through aliases, just add in your package.json as dependencies your internal package like this:
Stop. You are ready to create and use your monorepo. So simple no?
Fast CI/CD
This is a future point, right now we don’t use this technique so it’s a point we’re going to study in the future to see if it’s feasible and really leads to an improvement in the speed of our pipelines.
Having a common store of all our dependencies could help in creating pipelines (build or deploy) much faster than the current ones. Currently, we work using caches per project, so we don’t have to reinstall dependencies every time if they haven’t changed. But what if a dependency changes? Or changes the lock
file? The cache is invalidated and the process has to be redone. Extending this process with a common dependency store, maybe not just for projects but across all projects, could increase the speed of our pipelines even more. We will let you know if it is something feasible or not :)
I hope this brief roundup of information came in handy! Are you interested in finding out more about how we work? Follow us on medium or on weare.jobtome.com
We are always looking for new talents, if you like here you can find our open job positions:
If you liked the article please clap and follow :)
Thx and stay tuned 🚀
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK