WSL2 Ubuntu20.04中npm模块找不到及跨系统路径权限问题
WSL2 Ubuntu 20.04中npm模块找不到的权限问题
之前遇到过《In WSL2: Ubuntu 20.04 for Windows 10 nodejs is installed but npm is not working》里的问题,通过把换行符转成Unix格式解决了,但现在执行npm -v时出现以下错误:
mark@L-R910LPKW:~$ npm -v internal/modules/cjs/loader.js:883 throw err; ^ Error: Cannot find module '\\wsl.localhost\Ubuntu-20.04\mnt\c\Program Files\nodejs\node_modules\npm\bin\npm-cli.js' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15) at Function.Module._load (internal/modules/cjs/loader.js:725:27) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47 { code: 'MODULE_NOT_FOUND', requireStack: [] } mark@L-R910LPKW:~$
错误里的路径\\wsl.localhost\Ubuntu-20.04\mnt\c\Program Files\nodejs\node_modules\npm\bin\npm-cli.js,实际对应Windows系统的C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js,这个文件确实存在:
C:\> dir "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" Directory: C:\Program Files\nodejs\node_modules\npm\bin Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 4/6/2021 3:07 PM 4861 npm-cli.js C:\>
但从Windows端访问\\wsl.localhost\Ubuntu-20.04\mnt\c时会提示权限拒绝:
C:\> dir \\wsl.localhost\Ubuntu-20.04\mnt Directory: \\wsl.localhost\Ubuntu-20.04\mnt Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 10/6/2022 10:21 PM wsl d----- 10/7/2022 4:28 PM wslg d----- 10/7/2022 7:19 AM c C:\> dir \\wsl.localhost\Ubuntu-20.04\mnt\c dir : Access is denied At line:1 char:1 + dir \\wsl.localhost\Ubuntu-20.04\mnt\c + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (\\wsl.localhost\Ubuntu-20.04\mnt\c:String) [Get-ChildItem], Unauthor izedAccessException + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand C:\>
而/mnt下的其他目录(比如wsl、wslg)可以正常访问:
C:\> dir \\wsl.localhost\Ubuntu-20.04\mnt\wsl\ Directory: \\wsl.localhost\Ubuntu-20.04\mnt\wsl Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 10/6/2022 10:21 PM docker-desktop-bind-mounts d----- 10/6/2022 10:21 PM docker-desktop d----- 10/6/2022 10:21 PM docker-desktop-data ------ 10/6/2022 11:47 PM 122 resolv.conf C:\>
排查后发现权限差异:/mnt/c及其子目录归Linux普通账户mark所有,而其他目录归root账户:
mark@L-R910LPKW:/mnt$ ls -l total 0 drwxrwxrwx 1 mark mark 4096 Oct 7 07:19 c drwxrwxrwt 5 root root 120 Oct 6 22:21 wsl drwxrwxrwt 6 root root 260 Oct 7 16:28 wslg mark@L-R910LPKW:/mnt$
想知道正确的修复方法,让这个路径能从Windows正常访问。虽然可以把Windows路径从Linux的PATH里移除,但希望理解原理并彻底解决问题。
内容的提问来源于stack exchange,提问作者mark
相关产品推荐
相关产品推荐

