Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postinstall script is not being executed #5476

Open
aamorozov opened this issue Mar 7, 2018 · 45 comments
Open

Postinstall script is not being executed #5476

aamorozov opened this issue Mar 7, 2018 · 45 comments

Comments

@aamorozov
Copy link

aamorozov commented Mar 7, 2018

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Postinstall script specified in the package.json is not being executed after yarn add PACKAGE_NAME

If the current behavior is a bug, please provide the steps to reproduce.

If it can depend on the specifics of the script I can put together a repo to reproduce.

What is the expected behavior?
Postinstall script should be executed after yarn add installs the package. The script is being installed without any issues within npm install.

Please mention your node.js, yarn and operating system version.
node - 9.7.1
yarn - 1.5.1
MacOS Sierra

@ghost ghost assigned rally25rs Mar 7, 2018
@ghost ghost added the triaged label Mar 7, 2018
@rally25rs
Copy link
Contributor

From a quick test on OSX, Node 8.10, yarn 1.5.1 it seemed to work OK:

~/Projects/yarn-test/pkgB 🐒   cat ../pkgA/package.json
{
  "name": "pkgA",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "postinstall": "echo 'POST' > post.txt"
  }
}

~/Projects/yarn-test/pkgB 🐒   yarn add file:../pkgA
yarn add v1.5.1
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved 1 new dependency.
info Direct dependencies
└─ [email protected]
info All dependencies
└─ [email protected]
✨  Done in 0.14s.

~/Projects/yarn-test/pkgB 🐒   cat node_modules/pkgA/post.txt
POST

(adding pkgA to pkgB runs pkgA's postinstall script, creating post.txt)

Could you provide a full example?

@Tom-Bonnike
Copy link

Tom-Bonnike commented Mar 15, 2018

@rally25rs, your test seems to work fine.

But I can confirm that this is an issue with my package: clean-slate-lint.

Yarn:

$ yarn --version
1.5.1

$ yarn add -D clean-slate-lint
yarn add v1.5.1
warning package.json: No license field
warning [email protected]: No license field
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved 1 new dependency.
info Direct dependencies
└─ [email protected]
info All dependencies
└─ [email protected]
warning [email protected]: No license field
✨  Done in 2.43s.

No message and nothing was added to the package.json as expected with this package.
Content of the package.json here: https://github.com/Tom-Bonnike/clean-slate/blob/master/package.json

Npm:

$ npm i --save-dev clean-slate-lint

> [email protected] install /Users/tombonnike/Desktop/test/node_modules/husky
> node lib/installer/bin install

husky > setting up git hooks
husky > done

> [email protected] postinstall /Users/tombonnike/Desktop/test/node_modules/clean-slate-lint
> ./bin/post-install.js

✅  clean-slate post-commit hooks setup succeeded.

+ [email protected]
added 121 packages from 66 contributors, removed 62 packages and updated 500 packages in 43.483s

@Tom-Bonnike
Copy link

Tom-Bonnike commented Mar 15, 2018

As a smaller reproduction case:

$ cat ./pkgA/package.json
{
  "name": "pkgA",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "postinstall": "node test.js"
  }
}

$ cat ./pkgA/test.js
#!/usr/bin/env node
console.log('FOO BAR')

$ yarn add -D file:./pkgA
yarn add v1.5.1
warning package.json: No license field
warning [email protected]: No license field
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved 1 new dependency.
info Direct dependencies
└─ [email protected]
info All dependencies
└─ [email protected]
warning [email protected]: No license field
✨  Done in 0.24s.

FOO BAR isn’t logged.

@AviVahl
Copy link

AviVahl commented Apr 4, 2018

Yeah, happens for the latest sinon version as well.
npm i outputs:

> [email protected] postinstall C:\projects\postinstall\node_modules\sinon
> node scripts/support-sinon.js

Have some ❤️ for Sinon? You can support the project via Open Collective:
 > https://opencollective.com/sinon/donate

while with yarn, the message isn't shown. (I personally prefer not to see the message, heh; feels like an abuse in this case)

Could be that the postinstall runs, but output isn't connected to stdout?

@aamorozov
Copy link
Author

aamorozov commented Apr 16, 2018

As an update, the script is being executed for me with Yarn v. 1.6. However, the output from the script is not being populated within cli.

@nevir
Copy link

nevir commented May 30, 2018

Same thing on 1.7. The script is being run, but no stdout/stderr is visible (unless the script fails).

Is there a way for a postinstall script to flag that it wants some output printed as part of the regular install flow (e.g. advisory messages)?

@rotous
Copy link

rotous commented Feb 8, 2019

Using 1.12.3 but still no output from an install script is displayed. Any chance of this being changed?

@nebrelbug
Copy link

Please...

@rally25rs
Copy link
Contributor

There is a known issue where if a script's output doesn't contains an ending newline, then it's output gets overwritten by yarn's own output. It is possible your script is being run, just the output is not being displayed?

@nebrelbug
Copy link

I tried adding an ending newline, but still no luck. My code: https://github.com/nebrelbug/squirrelly/blob/master/postinstall.js

@nebrelbug
Copy link

@rally25rs so I don't believe that's the issue...

On a side note, is there a way to test postinstall scripts without publishing a new version every time?

@rainboxx
Copy link

FYI the postinstall script of full-icu is also not executed here. I'm on a Mac with:

> yarn -v
1.13.0
> node -v
v10.15.0

The way the postinstall script is specified looks totally valid: https://github.com/unicode-org/full-icu-npm/blob/756b48ba6ece2b14051bd9107501f90fd98811ba/package.json#L5-L7

@klintan
Copy link

klintan commented Apr 9, 2019

I'm at version 1.15.2 and this is still an issue.
The postinstall script does not run at all (not displayed and not run). Using npm install works as expected and the postinstall script is run as it should.

Anyone know if there is a solution in sight?

@Zycon42
Copy link

Zycon42 commented Apr 12, 2019

@rainboxx I'm at version 1.15.2 and full-icu works, yarn unfortunately swallows output from postinstall script but it's executed. full-icu prints info that you need to define NODE_ICU_DATA=./node_modules/full-icu environment variable to actually use it. They don't mention it anywhere in their README they rely on people seeing info from postinstall script but yarn users don't see it so to us it looks like it doesn't work.

@aamorozov
Copy link
Author

@rally25rs Here is a repo to reproduce - https://github.com/aamorozov/nightwatch-typescript/

Yarn version - 1.15.2
Node version - v11.14.0
Npm version - 6.9.0

@kangax
Copy link

kangax commented Jun 20, 2019

Using yarn 1.16.0 and my postinstall works but postuninstall doesn't 🤦‍♂

@kenk2
Copy link

kenk2 commented Oct 18, 2019

I'm using yarn 1.19.1 with node v12.11.1 and I'm still having this issue.

@Venryx
Copy link

Venryx commented Sep 12, 2021

It seems that yarn will execute the postinstall only if the related project really needs a installation, for example, the dependencies has changed, or the node_modules has been removed.

Can confirm that's the reason for me; I can get the postinstall script to run again if I change one of my dependency's versions. (a "fake" change [eg. where you just add/remove the ^ symbol] is not enough, if that does not change the "resolved version")

@ishan123456789
Copy link

Doesn't seem to be working with yarn v3.0.2 as well
"postinstall": "echo 'POST' > post.txt", doesn't run.

@jy617lee
Copy link

same as yarn v3.1.0. postinstall not excuted when excute yarn

root package.json

    "postinstall": "yarn workspaces foreach run postinstall"

package a package.json

    "typechain": "typechain --target web3-v1 --out-dir ./contract/interfaces './contract/abis/*.json'",
    "postinstall": "yarn typechain"

@katsenkatorz
Copy link

same as yarn v3.1.0. postinstall not excuted when excute yarn

root package.json

    "postinstall": "yarn workspaces foreach run postinstall"

package a package.json

    "typechain": "typechain --target web3-v1 --out-dir ./contract/interfaces './contract/abis/*.json'",
    "postinstall": "yarn typechain"

Thx @jy617lee work for me with yarn 3 and workspace 👍🏻

@jbreckmckye
Copy link

jbreckmckye commented Jan 11, 2022

Also chiming in: this is a potential problem for migrating a project that still has a Bower dependency. The issue is that yarn only re-runs postinstall when the package graph changes, but bower.json obviously isn't tracked as part of that.

There are some Berry plugins that implement always-ran postinstall scripts but they don't have a lot of development effort behind them - I would like to be able to rely on something in the core tool.

EDIT: My workaround was to write a small custom plugin that installed Bower as part of the install step.

@dawidk92
Copy link

I use node 16.14.2 in my project and yarn versions 2.x and 3.x don't run postinstall in my root package.json at all

@gulgulia17
Copy link

Yarn Version 1.22.19, It is working, My mistake was instead of putting the postinstall inside the script section, I put it outside.

"scripts": {
    "postinstall": "npx patch-package"
}

@damusix
Copy link

damusix commented Oct 19, 2022

Using yarn 3.2.1

postinstall hook only works on installs that actually have changes. Afterward, running yarn, yarn install, etc does nothing, which kind of defeats the purpose of this command, no?

@microHoffman
Copy link

I'm using Yarn 3.3.1 and commands in my postinstall hook do not get executed if there was nothing to install. Is there any workaround for this? In postinstall hook I am:

  • generating SDK from openapi schema
  • generating typechain typings.

Thanks!

@feng-yu-healthbank
Copy link

Using yarn 3.6.1.

"scripts": {
    "postinstall": "node ./bin/postInstall",
}

postinstall.js:

#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */

const childProcess = require('child_process');
const os = require('os');

/**
 * Do all things that need to be done after installing packages
 */
const iosCommands = [
  // on MacOS, fix images and install pods
  { command: 'npx react-native-fix-image' },
  { command: 'npx react-native setup-ios-permissions' },
  { command: 'npx pod-install' },
];

const commands = [
  // Patch libraries for typescript errors.
  { command: 'npx patch-package' },
  { command: 'npx jetify' },
  ...(os.platform() === 'darwin' ? iosCommands : []),
  // Make sure we're set up correctly
  { command: 'npx solidarity' },
];

commands.forEach(commandAndOptions => {
  const { command, ...options } = commandAndOptions;
  try {
    childProcess.execSync(command, {
      stdio: 'inherit',
      ...options,
    });
  } catch (error) {
    process.exit(error.status);
  }
});

Even if I deleted node_modules, running yarn install still didn't invoke postinstall. I need postinstall to invoke patch-package, jetify and fix-image.

@timeimp
Copy link

timeimp commented Sep 11, 2023

I had to resort to using Yarn Plugin After Install to have something fire after every yarn install call.

This issue has been open for over 5 years... so is the idea of "post install" meant to:

a) not work in Yarn (Classic)
b) not work in Yarn (Berry / 3+)

Could one of the Yarn project leads confirm if there is a real bug here (so we can work on getting some kind of fix) or if the intention is to replace this workflow with something different in newer versions of Yarn?

@cqh963852
Copy link

I found that If I'am using node16 ,the postinstall script won't work. But work on node18.

@The-Code-Monkey
Copy link

I have found this problem, So using PandaCss a prepare script is needed, i have tried using both prepare and postinstall,

neither work if there is already a yarn.lock & yarn/cache. meaning that if there was ever an update to the outputted files by PandaCss i wouldn't get those changes. this is a problem and really needs looking into. or at least a solution suggested by the @yarnpkg team.

@papb
Copy link

papb commented Mar 25, 2024

Ouch, it looks like an option like npm's --foreground-scripts still doesn't exist for yarn? I am trying to use DEBUG='*' yarn add --verbose prisma and I get nothing.

@sebastiantf
Copy link

Using yarn 3.2.1

postinstall hook only works on installs that actually have changes. Afterward, running yarn, yarn install, etc does nothing, which kind of defeats the purpose of this command, no?

Facing the same thing. Any workaround possible so far?

@gyihasz
Copy link

gyihasz commented Nov 11, 2024

Using yarn 3.2.1
postinstall hook only works on installs that actually have changes. Afterward, running yarn, yarn install, etc does nothing, which kind of defeats the purpose of this command, no?

Facing the same thing. Any workaround possible so far?

My general workaround suggestion is that you create an "install": "yarn install && yarn run postinstall" script in your package.json.
Then you can type yarn run install and it will execute both the "normal" install and the "postinstall" scripts.
Please note the 'run' in the middle. 😄

If you are too comfortable (like me) and don't want to type yarn RUN install but stick to yarn install only, then as an extra step, you can add this to your $HOME/[.bashrc, .zshrc .whateverrc] profile:

yarn() {
  if [[ "$1" == "install" ]]; then
    # Check if package.json exists and has an "install" script
    if [ -f package.json ] && grep -q '"install":' package.json; then
      command yarn run install
    else
      command yarn install
    fi
  else
    # Pass all other commands to yarn as usual
    command yarn "$@"
  fi
}

It checks if you have a script named "install" in your package.json file, and if you defined it, then it will execute that. If it doesn't find an "install" script, then it will execute the simple yarn install command which will not execute the postinstall.

So, if you really need a workaround:

  1. Add an "install": "yarn install && yarn run postinstall" line in your package.json scripts section
    • Live happily with yarn run install, or if you really want to have yarn install:
  2. Add the yarn install override script (see above) to your shell configuration

The second part of my suggestion worked for me and I'm happy with it, but it's so hacky that I'm really not sure if I should post this to the public. 😅 People will talk..
Not to mention that yarn install "looks for" a package.json in parent directories if it couldn't find one in the current working directory, while the overridden yarn install will only check the current working directory for a package.json and not even try to look for it before it 'gives up' and just executes the standard yarn install command.. 😄

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

No branches or pull requests