Skip to content

Instantly share code, notes, and snippets.

@PatrickChoDev
Created November 10, 2023 18:26
Show Gist options
  • Save PatrickChoDev/81d36159aca4dc687b8c89983e64da2e to your computer and use it in GitHub Desktop.
Save PatrickChoDev/81d36159aca4dc687b8c89983e64da2e to your computer and use it in GitHub Desktop.
PNPM devcontainer multiple project setup
{
"name": "Multiple PNPM projects",
// recommend using microsoft default image
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye",
// !!!! MAGIC STARTS HERE!!!
"mounts": [
"source=global-pnpm-store,target=${containerWorkspaceFolder}/.pnpm-store,type=volume"
],
"postCreateCommand": "sudo chown node .pnpm-store"
....
}
@PatrickChoDev
Copy link
Author

PNPM devcontainer multiple project setup

This is the useful script when you starting to create cross-containers pnpm setup.

Since I haven't seen someone raising this method on stack overflow yet.

You can change whatever in mount source (global-pnpm-store) or switch it if you wanted to use another env.

Why? Because It can safe a lot of disk space without using monorepo architecture. Because some project isn't that much related to be put in the same pnpm workspace but the dependencies does it.

Any recommendation or improvements is open.

@PatrickChoDev
Copy link
Author

Usage Example

Project 1

.devcontainer/devcontainer.json:

{
  "name": "Project 1",
  "image": "mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye",
  "mounts": [
    "source=global-pnpm-store,target=${containerWorkspaceFolder}/.pnpm-store,type=volume"
  ],
  "postCreateCommand": "sudo chown node .pnpm-store"
}

package.json:

{
  "dependencies": {
    "express": "*"   // Then run pnpm install on fresh .pnpm-store
  }
}

src/main.js:

const express = require("express/package.json").version;
console.log(express)   // Output : 4.18.2

This project seems to use latest express from npm.

Project 2

.devcontainer/devcontainer.json:

{
  "name": "Project 2",
  "image": "mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye",
  "mounts": [
    "source=global-pnpm-store,target=${containerWorkspaceFolder}/.pnpm-store,type=volume"
  ],
  "postCreateCommand": "sudo chown node .pnpm-store"
}

package.json:

{
  "dependencies": {
    "express": "^3.0.0"   // Then run pnpm install after project 1
  }
}

after running pnpm install on this project you will see...

 WARN  2 deprecated subdependencies found: connect@2.30.2, mkdirp@0.5.1
Packages: +91
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Progress: resolved 91, reused 18, downloaded 73, added 91, done

dependencies:
+ express 3.21.2 (4.18.2 is available)

Oh! it seems installed deprecated express@^3.0.0 library. Let's see on runtime.

src/main.js:

const express = require("express/package.json").version;
console.log(express)   // Output : 3.21.2

This project seems to use express version which defined in package.json which is what I expected to see from node with devcontainer!.

Let's craft your own one :)

Careful !!

Even it's work I recommend you not to touch .pnpm-store after creation. It might break if you delete some files in it. If it necessary or accident happen, you can simply run pnpm install in every single project that use this docker volume.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment