yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Postinstall script is not being executed

Open aamorozov opened this issue 7 years ago β€’ 49 comments

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

aamorozov avatar Mar 07 '18 17:03 aamorozov

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?

rally25rs avatar Mar 09 '18 03:03 rally25rs

@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 avatar Mar 15 '18 08:03 Tom-Bonnike

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.

Tom-Bonnike avatar Mar 15 '18 09:03 Tom-Bonnike

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?

AviVahl avatar Apr 04 '18 12:04 AviVahl

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.

aamorozov avatar Apr 16 '18 20:04 aamorozov

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)?

nevir avatar May 30 '18 00:05 nevir

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

rotous avatar Feb 08 '19 14:02 rotous

Please...

bgub avatar Feb 14 '19 03:02 bgub

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?

rally25rs avatar Feb 14 '19 13:02 rally25rs

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

bgub avatar Feb 15 '19 00:02 bgub

@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?

bgub avatar Feb 15 '19 20:02 bgub

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

rainboxx avatar Mar 18 '19 13:03 rainboxx

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?

klintan avatar Apr 09 '19 07:04 klintan

@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.

Zycon42 avatar Apr 12 '19 22:04 Zycon42

@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

aamorozov avatar Apr 16 '19 05:04 aamorozov

Using yarn 1.16.0 and my postinstall works but postuninstall doesn't πŸ€¦β€β™‚

kangax avatar Jun 20 '19 13:06 kangax

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

kenk2 avatar Oct 18 '19 03:10 kenk2

In my case it works. meaning, it executes the script but doesn't output. Which is okay for me.

tunnckoCore avatar Nov 06 '19 20:11 tunnckoCore

Not ideal, but in situations where you're guaranteed to always be in a *nix environment you could either make your install/postInstall script a shell script or shell out to the echo command where you would normally use something akin to console.log.

wickkidd avatar Nov 06 '19 21:11 wickkidd

Nothing happens with yarn workspace .. add ..

$ yarn -v 1.19.1

Bessonov avatar Nov 23 '19 21:11 Bessonov

In our workflow for a custom package of ours, we prompt the dev to make a decision on what to do with existing config files, during postinstall. We only run this if there are changes, so on first install, it's fine that there is no output. On subsequent installs, though, it's a significant issue that we're unable to prompt devs about how they'd like to handle the changes.

jackkoppa avatar Mar 12 '20 18:03 jackkoppa

@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?

you can install your package as following:

yarn add <path_to_your_package>

ChrisShen93 avatar Mar 16 '20 03:03 ChrisShen93

In my case, the postinstall script has been runned, but the output can not be displayed.

ChrisShen93 avatar Mar 16 '20 03:03 ChrisShen93

So postuninstall script can't stream to stdout even if in 2020? In my case this script will rebuild electron's pre-built package, and this message is useful for me to check whether the action success. The message is shown in postinstall.

mayeths avatar Apr 12 '20 14:04 mayeths

Same here. Yarn doesn't download electron binary while installing electron npm package.

axross avatar Jun 16 '20 22:06 axross

Just a mention: I was trying to do a typescript build on post install, as I'm working on a private package which is not published, but installed from our own repo and didn't want to commit the built files in there. Anyways, script runs just fine, but it seems to be executed asynchronously, as yarn reports it as an error even if actually runs successfully (like it doesn't wait for it to finish)

CatalinMustata avatar Jun 17 '20 09:06 CatalinMustata

I'm working on an monorepo project, with the help of yarn workspace and lerna. 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. Maybe it is by designed?

Besides, the postinstall execution does not pipe out any messages, too.

aprilandjan avatar Jul 31 '20 13:07 aprilandjan

+1 to @aprilandjan's comment. I created a sample repo here to reproduce the issue: jonohayon/yarn-workspaces-bug

Yarn version: 1.22.4 Node version: 12.18.2

jonohayon avatar Aug 09 '20 07:08 jonohayon

@jonohayon @aprilandjan

The way I worked around this was to add a postinstall script in my root package.json like this - "postinstall": "lerna run postinstall --stream", that called all the postinstall scripts in each package and ran it.

Then I just run yarn install from my root and it takes care of the install and the post-install.

mihirkothari25 avatar Sep 25 '20 18:09 mihirkothari25

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")

Venryx avatar Sep 12 '21 05:09 Venryx