pre-commit hooks use wrong node version

159 views Asked by At

in our Django project with React frontend we use pre-commit. When I create a commit containing java script files I get errors like

prettier.................................................................Failed
- hook id: prettier
- exit code: 1
prettier requires at least version 14 of Node, please upgrade

I am running on a Ubuntu derivative.

~/path/to/project$ whereis node
node: /usr/bin/node /usr/include/node ~/.nvm/versions/node/v16.20.0/bin/node /usr/share/man/man1/node.1.gz

$ which -a node
~/.nvm/versions/node/v16.20.0/bin/node
/usr/bin/node
/bin/node

$ which -a nodejs
/usr/bin/nodejs
/bin/nodejs

$ node -v 
v16.20.0

$ /usr/bin/node -v
v12.22.9

$ /bin/node -v
v12.22.9

$ nodejs -v
v12.22.9

$ /usr/bin/nodejs -v
v12.22.9

$ /bin/nodejs -v
v12.22.9

# .pre-commit-config.yaml

exclude: |
  (?x)(
    migrations/.*|
    static/dist/.*|
    static/dist/assets/.*|
    .svg|
    .drawio
  )
default_stages: [ commit ]

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-json
      - id: check-toml
      - id: check-xml
      - id: check-yaml
      - id: debug-statements
      - id: check-builtin-literals
      - id: check-case-conflict
      - id: check-docstring-first
      - id: detect-private-key

  - repo: https://github.com/PyCQA/flake8
    rev: 7.0.0
    hooks:
      - id: flake8

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: 'v1.7.1'
    hooks:
      - id: mypy
        args:
          - "--config-file=pyproject.toml"
        additional_dependencies:
          - Django~=4.2.9
          - django-filter~=23.2
          - djangorestframework~=3.14
          - drf-spectacular~=0.27.0
          - django-vite~=2.0
          - python-dotenv~=1.0.0
          - django-filter-stubs~=0.1.3
          - django-stubs~=4.2.7
          - django-stubs-ext~=4.2.7
          - djangorestframework-stubs~=3.14.1
          - Faker~=18.11
          - model-bakery~=1.12
          - django-extensions~=3.2.3
          - pygraphviz~=1.11

  - repo: https://github.com/psf/black
    rev: '23.12.1'
    hooks:
      - id: black

  - repo: https://github.com/pycqa/isort
    rev: 5.13.2
    hooks:
      - id: isort
        args: ["--profile", "black", "--filter-files"]

  - repo: https://github.com/pre-commit/mirrors-eslint
    rev: 'v8.41.0'
    hooks:
      - id: eslint
        files: \.[jt]sx?$
        types: [ file ]
        additional_dependencies:
          - [email protected]
          - [email protected]
  - repo: https://github.com/pre-commit/mirrors-prettier
    rev: 'v3.0.2'
    hooks:
      - id: prettier
        files: \.(js|jsx|ts|tsx|css|scss)$
        types: [ file ]

  - repo: local
    hooks:
      - id: build-frontend
        name: Build the frontend using npm
        entry: npm run build
        language: system
        types_or: ['ts', 'javascript', 'tsx']
        pass_filenames: false

I seems that pre-commit uses the system version of node in the hooks, BUT when I run pre-commit run --all-files everything works as expected.

Any ideas how to set pre-commit to use the correct node version?

Best regards

0

There are 0 answers