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

Strange behaviour after git hook is run (Error: Cannot find module 'turbo') #2147

Closed
nathnhughes opened this issue Oct 3, 2022 · 8 comments

Comments

@nathnhughes
Copy link

What version of Turborepo are you using?

1.5.5

What package manager are you using / does the bug impact?

Yarn v1

What operating system are you using?

Mac

Describe the Bug

We're using Husky to run a pre-commit hook before we commit changes to Github and have come across some strange behaviour.

The hook is just a single command:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn precommit

This runs our pre-commit script in our global package.json:

{
/* ... */
 "precommit": "turbo run precommit",
/* ... */
}

...which in turn runs lint-staged in both our ui lib and docs directories to prettify and lint the staged code.

After the pre-commit.sh script has run, you are no longer able to run any turbo CLI commands without first running yarn install. If you try to, you're presented with the following error:

(*local directory omitted for privacy*)

yarn lint

$ turbo run lint
/<local-directory>/node_modules/turbo/bin/turbo:13
    throw e;
    ^

Error: Cannot find module 'turbo'
Require stack:
- /<local-directory>/node_modules/turbo/node-platform.js
- /<local-directory>/node_modules/turbo/bin/turbo
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at downloadedBinPath (/<local-directory>/node_modules/turbo/node-platform.js:44:44)
    at generateBinPath (/<local-directory>/node_modules/turbo/node-platform.js:67:15)
    at Object.<anonymous> (/<local-directory>/node_modules/turbo/bin/turbo:7:5)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/<local-directory>/node_modules/turbo/node-platform.js',
    '/<local-directory>/node_modules/turbo/bin/turbo'
  ]
}

For what it's worth, here's the spec of the machine I'm using:

  Model Name: MacBook Pro
  Model Identifier: MacBookPro18,2
  Chip: Apple M1 Max
  Total Number of Cores: 10 (8 performance and 2 efficiency)
  Memory: 64 GB
  System Firmware Version: 7459.141.1
  OS Loader Version: 7459.141.1

Expected Behavior

Should be able to run turbo commands without having to re-install.

To Reproduce

To reproduce:

  1. clone the following repo:
git clone https://github.com/nathnhughes/turborepo-husky-unistall-issue.git
  1. Make some changes to the files
  2. Commit these changes
  3. Try to run a turbo command (e.g. yarn lint) from the root directory.
@ThaddeusJiang
Copy link

I had the same issue too.

@nhducit
Copy link

nhducit commented Jan 16, 2023

it might related to #3317

@sreekanthjnayar
Copy link

sreekanthjnayar commented Jan 24, 2023

The problem seem to be a case where the TURBO_BINARY_PATH is not being constructed correctly while triggering the command from a shell script (from a pre-commit script involving Husky) or while invoking the turbo command from a child process.
A workaround seems to be to manually set the TURBO_BINARY_PATH in your husky pre-commit script,
export TURBO_BINARY_PATH=<path_to_turbo_based_on_your_system_architecture>
For example, I use a M processor based Mac and I ended up setting the value to node_modules/turbo-darwin-arm64/bin/turbo (the full path).
This is not a final fix, maybe you could extend it to get the location of the binary programatically; I was generally curious to see what the issue was. I guess we'll need help from the Turbo repo team on this.

@tknickman
Copy link
Member

@nathnhughes if you upgrade to turbo@1.7, you can install turbo globally, and then your precommit could simply be turbo precommit.

@joelybahh
Copy link

@nathnhughes if you upgrade to turbo@1.7, you can install turbo globally, and then your precommit could simply be turbo precommit.

This solved it for us, we were having various cross-platform development issues, everyone on windows encountered this bug, and everyone on the latest OS for macbook. By upgrading to latest and installing it globally instead, we now have every platform running turbo, and all husky/git hooks run without this platform-specific error.

@joelybahh
Copy link

Interestingly, in every case of this error, it only occurred when turbo was called from the git hook (and turbo was local dep), the same command could be ran from the terminal error-free

@nathnhughes
Copy link
Author

@tknickman @joelybahh can confirm that this did appear to fix the issue.

Perhaps something worth drawing peoples attention to in the docs if this is to be the workaround going forward.

@nathanhammond
Copy link
Contributor

This should be addressed by #5695. If you see this again please open a new issue!

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

7 participants