2

I am working on a React monorepo and I have the below scripts in the root package.json:

"scripts": {
    "build": "lerna run build",
},
"husky": {
    "hooks": {
      "pre-commit": "lint-staged",
    }
  },

lint-staged.config.js

module.exports = {
  '**/*.*': 'yarn build',
};

When I commit the code, the commit fails with the below error:

✖ yarn build:
ERR! lerna Unknown arguments: /Users/xyz/lint-staged.config.js, /Users/xyz/package.json
error Command failed with exit code 1.
$ lerna run build /Users/xyz/lint-staged.config.js /Users/xyz/package.json
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky > pre-commit hook failed (add --no-verify to bypass)

In the monorepo packages folder, the package.json contains the scripts as below: package/webApp/package.json

"scripts": {
    "build": "run-s clean compile",
    "clean": "rm -rf *.tsbuildinfo & rm -rf build && rm -rf tmp",
    "compile": "run-p compile:*",
}

Can't we run the build command in lint-staged or is something missing in my implementation?

Thanks

newbie
  • 530
  • 1
  • 9
  • 36
  • Why would you WANT to run the `build` command with `lint-staged`? It's for linting files, not building them. The most typical config for a typescript project would be `{ "*.ts": "eslint" }`, which says "for all of the `.ts` files that have been changed, run `eslint file1.ts file2.ts`. What you have currently would match all files, not just the lintable ones, and then execute e.g. `yarn build /Users/xyz/lint-staged.config.js /Users/xyz/package.json`, which would in turn call `lerna run build /Users/xyz/lint-staged.config.js /Users/xyz/package.json`, which is what the error means. – Eric Haynes Jul 11 '22 at 22:51

1 Answers1

1

lint-staged is passing filenames to the build script, whereas lerna thinks these are being arguments and can't resolve them. To avoid passing the filenames to the build script, try to change your lint-staged configuration to the following

// lint-staged.config.js

module.exports = {
  '**/*.*': () => 'yarn build',
};

Here is the link to a similar example in the lint-staged - Link

Dzianis Roi
  • 853
  • 4
  • 12