yarn icon indicating copy to clipboard operation
yarn copied to clipboard

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

Open kripod opened this issue 6 years ago • 7 comments

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

kripod avatar Nov 16 '19 23:11 kripod

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

AiTranXuan avatar Dec 30 '19 02:12 AiTranXuan

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

alejandroclaro avatar Jan 15 '20 13:01 alejandroclaro

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

Ravnurin avatar Jan 28 '20 14:01 Ravnurin

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

Kadeluxe avatar Aug 23 '20 13:08 Kadeluxe

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 ?

cdegalitt avatar Sep 15 '20 12:09 cdegalitt

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 avatar Dec 16 '20 17:12 starikcetin

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

miguelcast avatar Jul 15 '24 23:07 miguelcast