Skip to content

Commit 240f2cb

Browse files
authored
Merge pull request #731 from jupyter-lsp/add-dot-local-node-root
Check for `node_modules` in `~/.local/lib` or other user-defined prefix
2 parents 4c7cb7c + acd76b7 commit 240f2cb

File tree

1 file changed

+22
-0
lines changed
  • python_packages/jupyter_lsp/jupyter_lsp

1 file changed

+22
-0
lines changed

python_packages/jupyter_lsp/jupyter_lsp/types.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import pathlib
77
import re
88
import shutil
9+
import subprocess
910
import sys
11+
from functools import lru_cache
1012
from typing import (
1113
TYPE_CHECKING,
1214
Any,
@@ -230,6 +232,17 @@ def _default_nodejs(self):
230232
shutil.which("node") or shutil.which("nodejs") or shutil.which("nodejs.exe")
231233
)
232234

235+
@lru_cache(maxsize=1)
236+
def _npm_prefix(self):
237+
try:
238+
return (
239+
subprocess.run(["npm", "prefix", "-g"], check=True, capture_output=True)
240+
.stdout.decode("utf-8")
241+
.strip()
242+
)
243+
except Exception as e: # pragma: no cover
244+
self.log.warn(f"Could not determine npm prefix: {e}")
245+
233246
@default("node_roots")
234247
def _default_node_roots(self):
235248
"""get the "usual suspects" for where `node_modules` may be found
@@ -257,6 +270,15 @@ def _default_node_roots(self):
257270
# ... but right in %PREFIX% on nt
258271
roots += [pathlib.Path(sys.prefix)]
259272

273+
# check for custom npm prefix
274+
if shutil.which("npm"):
275+
prefix = self._npm_prefix()
276+
if prefix:
277+
roots += [ # pragma: no cover
278+
pathlib.Path(prefix) / "lib",
279+
pathlib.Path(prefix),
280+
]
281+
260282
return roots
261283

262284

0 commit comments

Comments
 (0)