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

Error when using 'postinstall' in a yarn workspace's package #7694

Open
kripod opened this issue Nov 16, 2019 · 7 comments
Open

Error when using 'postinstall' in a yarn workspace's package #7694

kripod opened this issue Nov 16, 2019 · 7 comments

Comments

@kripod
Copy link

kripod commented Nov 16, 2019

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

I'm reporting a bug.

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

When using Yarn workspaces with the configuration below:

// root package.json
{
  "private": true,
  "workspaces": [
    "client",
    "server"
  ],
  // ...
}

The postinstall script inside server/package.json cannot run properly. It tries to invoke a binary of its own dependency, as seen below:

server/package.json
{
  "private": true,
  "name": "server",
  "version": "0.0.0",
  "scripts": {
    "postinstall": "prisma2 generate"
  },
  "dependencies": {
    "prisma2": "^2.0.0-preview016.2"
  }
}

And the following error gets thrown when executing yarn from the root directory:

[4/4] Building fresh packages...
error D:\Development\Projects\app\node_modules\server: Command failed.
Exit code: 1
Command: prisma2 generate
Arguments:
Directory: D:\Development\Projects\app\node_modules\server
Output:
internal/modules/cjs/loader.js:783
    throw err;
    ^

Error: Cannot find module 'D:\Development\Projects\app\node_modules\node_modules\prisma2\build\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:780:15)
    at Function.Module._load (internal/modules/cjs/loader.js:685:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

As it can be seen, it tries to invoke D:\Development\Projects\app\node_modules\node_modules\prisma2\build\index.js, which does not exist. When running yarn from the 'server' directory, everything works as expected.

I think that before executing postinstall scripts for each package, the current directory should be modified to simulate execution straight from the package instead of the workspace root.

Please mention your node.js, yarn and operating system version.

Node.js: 12.11.1
Yarn: 1.19.1
Windows 10 x64

@AiTranXuan
Copy link

Same Error and I using:
Node:12.14.0
Yarn:1.21.1
Macos 10.15.2 (19C57)

@alejandroclaro
Copy link

Same Error when try to execute electron-rebuild in the postinstall of a package. This only happens in windows. In Linux is working properly.

@Ravnurin
Copy link

Experiencing same problem on Windows 10, with postinstall trying to find the module under \node_modules\node_modules\....

@Kadeluxe
Copy link

What is the state of this? I can't execute tsc from postinstall script, same error.

@cdegalitt
Copy link

cdegalitt commented Sep 15, 2020

Hi,

I'm facing the same issue here.

Environment:

  • OS: Windows 10.0.19041.508 (2004)
  • Shell: GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)

Description:
I stumbled upon this after changing my package names to scoped package names (in the form @company/package).

  • Before using scoped package names, I had no issue with yarn install.
  • I edited my packages to use scoped package names, in their respective package.json files
  • Now, when i yarn install, one of my sub-packages (using a scoped package name) does not resolve its own dependency to tsc properly.

Trace:

[4/4] Building fresh packages...
error C:\Projects\galitt-l3-tse\node_modules\@galitt\exception: Command failed.
Exit code: 1
Command: run-s build
Arguments:
Directory: C:\Projects\galitt-l3-tse\node_modules\@galitt\exception
Output:
yarn run v1.22.5
$ echo $$NPM_EXEC_PATH && run-s build:code build:types
$$NPM_EXEC_PATH
$ echo $$NPM_EXEC_PATH && tsc
$$NPM_EXEC_PATH
internal/modules/cjs/loader.js:1033
  throw err;
  ^

Error: Cannot find module 'C:\Projects\galitt-l3-tse\node_modules\node_modules\typescript\bin\tsc'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1030:15)
    at Function.Module._load (internal/modules/cjs/loader.js:899:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
error Command failed with exit code 1.

Some more details:
I noticed wrappers to tsc seems to have been created inside the sub-package's node_modules by yarn install.

Contents of /c/Projects/galitt-l3-tse/node_modules/@galitt/exception/node_modules/.bin/tsc below:

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  "$basedir/node"  "$basedir/../../../../typescript/bin/tsc" "$@"
  ret=$?
else 
  node  "$basedir/../../../../typescript/bin/tsc" "$@"
  ret=$?
fi
exit $ret

It seems that the path %~dp0\..\..\..\..\ resolves to the root node_modules folder, and the wrapper script is appending node_modules to it.

Hence we lookup for the binary inside ...\node_modules\node_modules\... instead of ...\node_modules\...

One can notice there is also a windows shell script named tsc.cmd that follows the same (flawed ?) logic.

Fun fact:
When I cd into /c/Projects/galitt-l3-tse/node_modules/@galitt/exception and execute yarn postinstall manually, it works just fine.

Question:
Does somebody have any idea how those wrappers are generated ? And if it is possible how to fix them so they can properly implement the node module resolution algorithm by recursively looking upwards ?

@starikcetin
Copy link

starikcetin commented Dec 16, 2020

Same issue here, it is trying to resolve to root\node_modules\node_modules\package instead of root\node_modules\package.

Windows 10
Node 12.0.0
npm 6.9.0
yarn 1.22.10

Putting the postinstall scripts at the root package.json solved the issue for me, but ideally, I would want them localized.

starikcetin added a commit to ctisbtes/btes that referenced this issue Dec 16, 2020
putting `postinstall`s inside packages' own `package.json` gives weird errors.
see: yarnpkg/yarn#7694 (comment)
starikcetin added a commit to ctisbtes/btes that referenced this issue Feb 5, 2021
1. Yarn workspaces has problems dealing with `postinstall` scripts ( see: yarnpkg/yarn#7694 )

2. I don't remember the reason I added them in the first place.

Only `backend` had one and it was running the `generate` script. Since `generate` is being run before every launch already, it is unnecessary to run it after an install.
@miguelcast
Copy link

I have the same problem, but trying to postinstall in my monorepo Husky o Lefthook in windows with yarn 1.22.22

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

No branches or pull requests

8 participants