Skip to content

Instantly share code, notes, and snippets.

@0x0a0d
Created September 29, 2021 17:55
Show Gist options
  • Save 0x0a0d/a22bdb57c736c8f0adc9209295aff7ed to your computer and use it in GitHub Desktop.
Save 0x0a0d/a22bdb57c736c8f0adc9209295aff7ed to your computer and use it in GitHub Desktop.
`Lerna link` and `patch-package`

For anyone who stuck after run lerna link, patch-package will not be executed at lerna bootstrap

  1. At root path (same location with lerna.json), edit package.json as below
// ...
  "devDependencies": {
    "@lerna/child-process": "^4.0.0",
    "@lerna/project": "^4.0.0",
    // ...
  },
  "scripts": {
    "postinstall": "node bootstrap.js"
  }
  1. Create bootstrap.js at root path
// bootstrap.js
const fs = require('fs')
const path = require('path')
const { getPackagesSync } = require('@lerna/project')
const { spawnStreaming } = require('@lerna/child-process')

const packages = getPackagesSync()
packages
  .forEach(pkg => {
    const patches = path.join(pkg.location, 'patches')
    if (fs.existsSync(patches)) {
      spawnStreaming('node', [require.resolve('patch-package'), '--patch-dir', path.relative(__dirname, patches)], { shell: true })
    }
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment