Postinstall script is not being executed
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
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, 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
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.
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?
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.
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)?
Using 1.12.3 but still no output from an install script is displayed. Any chance of this being changed?
Please...
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?
I tried adding an ending newline, but still no luck. My code: https://github.com/nebrelbug/squirrelly/blob/master/postinstall.js
@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?
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
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?
@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.
@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
Using yarn 1.16.0 and my postinstall works but postuninstall doesn't π€¦ββ
I'm using yarn 1.19.1 with node v12.11.1 and I'm still having this issue.
In my case it works. meaning, it executes the script but doesn't output. Which is okay for me.
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.
Nothing happens with yarn workspace .. add ..
$ yarn -v 1.19.1
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.
@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>
In my case, the postinstall script has been runned, but the output can not be displayed.
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.
Same here. Yarn doesn't download electron binary while installing electron npm package.
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)
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.
+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 @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.
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")