Search code examples
reactjslernahuskylint-staged

lerna run build command fails in lint-staged


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


Solution

  • 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