Initial setup for pnpm and Node version management
This lesson preview is part of the Bundling and Automation in Monorepos course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Bundling and Automation in Monorepos, plus 90+ \newline books, guides and courses with the \newline Pro subscription.

[00:00 - 00:10] We're going to start in an empty folder in my home directory called monorepo. It has nothing in it. So what we're going to do first is do a "git init" and then we're going to open package.json.
[00:11 - 00:22] We're going to add a "name" field of "monorepo", and then a "version" of "1.0.0". "name" and "version" are the only required fields in package.json, but we're going to add one more.
[00:23 - 00:32] We're just going to set our package to "private" so that it's not publishable to npm. You would always want to set your monorepo root to private.
[00:33 - 00:55] Next, we are going to open a file called .nvmc and just write the number 22 in it, which signifies that we want Node version 22. We're going to commit our changes, and then we're going to look at the files we have, which is for now just package.json and .nvmc.
[00:56 - 01:11] With that, we can run "nvm install" which is going to install the latest version of Node 22, which right now is Node 22.3.0. So let's look at what binaries we got from this installation.
[01:12 - 01:18] We can do this with "which Node" and then like "dirname $(which Node)". And let's look at the folder for that directory.
[01:19 - 01:31] We have the binaries for Node as well as for npm and corepack. But what is corepack? It says it has a version of zero point something.
[01:32 - 01:38] But what does it actually do? We're going to use a package called "tldr" that's on npm.
[01:39 - 02:00] So we're going to run "npx tldr corepack". So corepack is a zero-runtime-dependency package acting as a bridge between Node and package managers. And when we run "corepack enable" we're going to get binaries now for pnpm and yarn added by corepack.
[02:01 - 02:10] We can look at the source code of these binaries. And we see that internally they call corepack, which means they're not the real pnpm, they're not the real yarn.
[02:11 - 02:38] But it goes to a shell of corepack, and then it runs the underlying package manager. In order to utilize corepack, we can open package.json and add a "packageManager" field. We're going to set a value of "[email protected]" and then when we try to run pnpm in the command line, we're going to actually get that exact version 9.0.0.
[02:39 - 02:50] We can open package.json again and change the version to 9.4.0. And then when we run pnpm again, we'll see that exact version.
[02:51 - 03:08] So what it does is, it will give you a package manager installation that exactly matches whatever you have specified in your package.json's "packageManager" field. So let's add this to git and let's commit our changes.
[03:09 - 03:27] We can look at what we've done here, which is just to add this "packageManager" field, and we now have a pnpm lock. So we'll commit this as "Add package.json#packageManager specifying pnpm version." The next part we're going to do is add some limitation to the version of Node that we allow in our monorepo.
[03:28 - 03:41] We're going to add an engines field and set a Node version of greater than or equal to 22.0.0. So now when we run "pnpm install" it obviously runs correctly.
[03:42 - 04:27] But if we switch our Node version to, let's say version 20 and try to run "pnpm install" again, we're going to get this warning that says, hey, you don't match this. The expectation of this package engines field, which is a good way to help everyone that uses our monorepo use the correct version of Node that we support. Let's commit this as "Add engine requirement for Node to be over version 22.0.0". So what we currently have here, if we look at .nvmrc, package.json, and pnpm-lock.yaml is the absolute minimum setup to get Node and pnpm versions managed by our Monorepo.
[04:28 - 04:41] Now let's summarize what we did for new users of our Monorepo in terms of how do you do initial setup? We're going to create a README.md file. We're going to say that this is our company's Monorepo.
[04:42 - 05:04] We have only one requirement to use this Monorepo is that you must have NVM installed, which is up to the user to figure out. And then the commands that we give the user in order to do the initial setup is to run npm install, then to run core pack, enable and finally to run pnpm install.
[05:05 - 05:14] We're going to add a short comment to each of those commands so that they're clear for first time users. And that's our initial setup done.