Next.js Discord

Discord Forum

Husky Lint-Staged pre-commit failing

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
Hello all, I've been bashing my head against the wall on this since last night. It's only happening in my Next.js app, not any other React/TypeScript non-Next.js apps.

When I commit changes, Husky's pre-commit fails on lint with the following, treating files as directories. Running pnpm lint outside of the pre-commit works just fine.

Error: ENOTDIR: not a directory, open '<app-directory>/package.json/tsconfig.json'

Here are the relevant files:

tsconfig.json

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}


.eslintrc.js

import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { FlatCompat } from '@eslint/eslintrc';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const compat = new FlatCompat({
  baseDirectory: __dirname,
});

const eslintConfig = [
  ...compat.extends(
    'next/core-web-vitals',
    'next/typescript',
    'prettier',
    'plugin:prettier/recommended',
    'plugin:@typescript-eslint/recommended',
  ),
  {
    rules: {
      '@typescript-eslint/no-explicit-any': 'error',
      '@typescript-eslint/no-non-null-assertion': 'off',
      '@typescript-eslint/no-var-requires': 'off',
      'no-console': 2,
    },
  },
];

export default eslintConfig;


pre-commit

pnpm lint-staged


package.json

{
  "scripts": {
    "preinstall": "npx only-allow pnpm",
    "prepare": "husky",
    "format": "prettier --write .",
    "lint": "next lint"
  },
  "lint-staged": {
    "*/**/*.{js,jsx,ts,tsx,json}": [
      "pnpm format",
      "pnpm lint"
    ]
  },
}
Answered by Masai Lion
Welp, solved the issue by changing pnpm lint to npx eslint --fix in the package.json's lint-staged section, and then changing .eslintrc.js to eslint.config.json - previous ESLint filename was throwing a different error that the config wasn't found. So, something about next lint that the Husky pre-commit process does not like.
View full answer

1 Reply

Masai LionOP
Welp, solved the issue by changing pnpm lint to npx eslint --fix in the package.json's lint-staged section, and then changing .eslintrc.js to eslint.config.json - previous ESLint filename was throwing a different error that the config wasn't found. So, something about next lint that the Husky pre-commit process does not like.
Answer