lint-staged: Error: fatal: Not a git repository

When trying to use lint-staged and husky in a project where the actual node.js project is a child directory of the git repo (as shown below):

project
  └─ node-app
       └─ package.json

I get the following error:

:; git commit                                                                                                                  

> husky - npm run -s precommit

Error: fatal: Not a git repository: '.git'

    at ChildProcess.<anonymous> (/home/tri/project/node-app/node_modules/staged-git-files/index.js:85:19)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:501:12)
/home/tri/project/node-app/node_modules/lint-staged/src/index.js:44
            files.forEach((file) => {
                 ^

TypeError: Cannot read property 'forEach' of undefined
    at sgf (/home/tri/project/node-app/node_modules/lint-staged/src/index.js:44:18)
    at /home/tri/project/node-app/node_modules/staged-git-files/index.js:13:13
    at /home/tri/project/node-app/node_modules/staged-git-files/index.js:38:13
    at ChildProcess.<anonymous> (/home/tri/project/node-app/node_modules/staged-git-files/index.js:88:9)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)

When running npm run precommit, I see a lot of these errors

:; npm run precommit

> node-app@1.0.0 precommit /home/tri/project/node-app
> lint-staged

 ❯ Running tasks for *.js
   ✖ prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write
     → /home/tri/prooject/node-app/node-app/Link.react.js
     git add
🚫 prettier --single-quote --no-bracket-spacing --jsx-bracket-same-line --write  found some errors. Please fix them and try committing again.
Unable to read file:
Error: ENOENT: no such file or directory, open ''
Unable to read file: /home/tri/project/node-app/node-app/test/nightwatch/user.js
Error: ENOENT: no such file or directory, open '/home/tri/project/node-app/node-app/test/nightwatch/user.js'
Unable to read file: /home/tri/project/node-app/node-app/test/nightwatch/universalSearch.js

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 23

Most upvoted comments

That’s why I encourage people to read the README when something isn’t working 😃

@maxime1992 You have to insert gitDir as property of lint-staged. like this.

{
  "lint-staged": {
    "gitDir": "../",
    "*.ts": [
      "yarn run prettier:precommit",
      "git add"
    ]
  },
  ...
}

@sudo-suhas looks like the precommit passed. It was the postcommit that threw the error. I checked my .git folder, and the hooks are installed correctly. Below is my --debug output

Running lint-staged with the following config:
{
  gitDir: '../',
  linters: {
    '*.{js,jsx}': [
      'eslint --fix',
      'git add'
    ]
  },
  concurrent: true,
  chunkSize: 9007199254740991,
  globOptions: {
    matchBase: true,
    dot: true
  },
  ignore: [],
  subTaskConcurrency: 1,
  renderer: 'verbose'
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:run Resolved git directory to be `/Users/yiouchen/Documents/workspace/project/` +1ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'UIModules/yarn.lock', 'UIModules/package.json' ] +104ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:cfg Normalizing config +111ms
  lint-staged:gen-tasks Generated task:
  lint-staged:gen-tasks { pattern: '*.{js,jsx}',
  lint-staged:gen-tasks   commands: [ 'eslint --fix', 'git add' ],
  lint-staged:gen-tasks   fileList: [] } +20ms
Running tasks for *.{js,jsx} [started]
Running tasks for *.{js,jsx} [skipped]
→ No staged files match *.{js,jsx}
  lint-staged linters were executed successfully! +140ms
husky > npm run -s postcommit (node v8.5.0)


> veeva-ui-modules@ postcommit /Users/yiouchen/Documents/workspace/vault/UIModules
> git update-index --again

fatal: Not a git repository: '.git'

@MJ111 this won’t work. You need to use advanced config with linters key. See README

Ah, I think I put that gitDir config as sibling to lint-staged, not as a property of. My bad. Thanks for your help.