Repository: pre-commit/pre-commit

Issue:
Executable `prettier` not found
hello hello!

As discussed on discord, I'm having an issue running prettier via pre-commit:

```bash
$ pre-commit --version
pre-commit 2.5.1

$ cat .pre-commit-config.yaml
repos:
  - repo: https://github.com/prettier/prettier
    rev: 2.0.5
    hooks:
      - id: prettier

$ pre-commit clean
Cleaned /home/rkm/.cache/pre-commit.

> pre-commit run prettier --files README.md
[INFO] Initializing environment for https://github.com/prettier/prettier.
[INFO] Installing environment for https://github.com/prettier/prettier.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
prettier.................................................................Failed
- hook id: prettier
- exit code: 1

Executable `prettier` not found
```

it seems like prettier is installed correctly, but the symlink to it is not created:

```bash
$ find ~/.cache/pre-commit/ -name prettier.js
/home/rkm/.cache/pre-commit/repoes79dg4v/bin/prettier.js

$ ls -l $(find ~/.cache/pre-commit/ -name node_env-default)/bin
total 70376
-rwxr-xr-x. 1 rkm rkm     3702 Jun 17 17:30 activate
-rwxr-xr-x. 1 rkm rkm     3964 Jun 17 17:30 activate.fish
-rwxr-xr-x. 1 rkm rkm 72052312 Jun  2 14:33 node
lrwxrwxrwx. 1 rkm rkm        4 Jun 17 17:30 nodejs -> node
lrwxrwxrwx. 1 rkm rkm       38 Jun 17 17:30 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx. 1 rkm rkm       38 Jun 17 17:30 npx -> ../lib/node_modules/npm/bin/npx-cli.js
-rwxr-xr-x. 1 rkm rkm      355 Jun 17 17:30 shim 
```

(doing the same in a docker container results in a `prettier` symlink being created there).

I suspect my VM may be borked somehow, but not sure how to debug this further. Any thoughts? Thanks!



Discussion:
thrugh some digging on discord we found the following:

```
> env | grep -i npm
NPM_CONFIG_USERCONFIG=/home/rkm/.config/npm/npmrc
```

and

```
> cat ~/.config/npm/npmrc
prefix=${XDG_DATA_HOME}/npm
cache=${XDG_CACHE_HOME}/npm
tmp=${XDG_RUNTIME_DIR}/npm
init-module=${XDG_CONFIG_HOME}/npm/config/npm-init.js
```

which is probably the issue
ok, so I did not have `XDG_CACHE_HOME` set and npm seemed to be getting confused (instead of defaulting this to `~/.cache` like most other tools do). prettier now works after re-installing!

I'm not sure if we want to do any more debugging on how exactly this broke, but I'm happy to close. thanks again!
I think I can actually make pre-commit avoid this problem case independent of a misconfigured system, I'd like to try and do that first

Resolving patch:
diff --git a/pre_commit/languages/node.py b/pre_commit/languages/node.py
--- a/pre_commit/languages/node.py
+++ b/pre_commit/languages/node.py
@@ -10,6 +10,7 @@
 from pre_commit import parse_shebang
 from pre_commit.envcontext import envcontext
 from pre_commit.envcontext import PatchesT
+from pre_commit.envcontext import UNSET
 from pre_commit.envcontext import Var
 from pre_commit.hook import Hook
 from pre_commit.languages import helpers
@@ -56,6 +57,8 @@ def get_env_patch(venv: str) -> PatchesT:
         ('NODE_VIRTUAL_ENV', venv),
         ('NPM_CONFIG_PREFIX', install_prefix),
         ('npm_config_prefix', install_prefix),
+        ('NPM_CONFIG_USERCONFIG', UNSET),
+        ('npm_config_userconfig', UNSET),
         ('NODE_PATH', os.path.join(venv, lib_dir, 'node_modules')),
         ('PATH', (bin_dir(venv), os.pathsep, Var('PATH'))),
     )


Test patch:
diff --git a/tests/repository_test.py b/tests/repository_test.py
--- a/tests/repository_test.py
+++ b/tests/repository_test.py
@@ -260,6 +260,14 @@ def test_run_versioned_node_hook(tempdir_factory, store):
     )
 
 
+@xfailif_windows  # pragma: win32 no cover
+def test_node_hook_with_npm_userconfig_set(tempdir_factory, store, tmpdir):
+    cfg = tmpdir.join('cfg')
+    cfg.write('cache=/dne\n')
+    with mock.patch.dict(os.environ, NPM_CONFIG_USERCONFIG=str(cfg)):
+        test_run_a_node_hook(tempdir_factory, store)
+
+
 def test_run_a_ruby_hook(tempdir_factory, store):
     _test_hook_repo(
         tempdir_factory, store, 'ruby_hooks_repo',