How to run a postinstall script after
Asked Answered
D

1

6

I am migrating from yarn version 1.22 to yarn v3.2.3. I have a prepare script that was running automatically after install.

My prepare script generates some files (FIY Nuxt 3 generated files).

It does not run anymore on yarn 3, and how can I get the same effect? I don't find it on https://yarnpkg.com/advanced/lifecycle-scripts. Maybe I am misreading.

Deberadeberry answered 27/9, 2022 at 9:37 Comment(2)
Did you ever resolve this? I'm running into the same issue.Ruscher
@Ruscher I posted itDeberadeberry
D
12

I have finally found a solution:

  1. Install the Yarn plugin after install: yarn plugin import https://raw.githubusercontent.com/mhassan1/yarn-plugin-after-install/v0.3.1/bundles/@yarnpkg/plugin-after-install.js.
  • See the command in the README to get the latest version.
  1. In a .yarnrc.yml at the root of your project, add your command: afterInstall: <your-command>
Deberadeberry answered 9/12, 2022 at 18:54 Comment(3)
is there anything similar for pre-install?Dismissive
@AkshayGundewar I found a way to essentially add a preinstall script: Simply create an empty NodeJS script somewhere (no exports needed), then reference it in .yarnrc.yml as a plugin: plugins: [".yarn/plugins/preinstall.js"]. Then just put your preinstall logic in that js file. Yarn imports from this script (looking for a plugin configuration) early enough in the process that, eg. if you make changes to the package.json file, those changes are applied before yarn reads from it. (or at least before it does the contents read for then modifying the contents and re-saving it)Seraphine
Oh, if you want to make sure the preinstall script only runs before an actual install, wrap your preinstall code in this if condition: if (process.argv[2] == "install" || process.argv[2] == null) { ... }Seraphine

© 2022 - 2024 — McMap. All rights reserved.