wtto WTTO'S BLOG

node resolve module not working when using link path

Posted on: 2023年4月1日  at 13:16
node resolve module not working when using link path

Failed to resolve module specifier “vue”

Windows 系统上开发一个 vue 项目,在启动时,碰到了问题,浏览器报错如下

Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../".

看起来像是 import moudle 的时候,没有去 node_modules 目录下寻找对应的模块。

然后我去 Google 了好久,发现了一个 issue,看起来问题一样,但是并没有解决我的问题。

然后我去调试 node_modules 目录下的 vite 模块,打断点,却发现在 vscode 的调试模式下,项目运行完全正常,浏览器没有报错。

然后我去掉断点,依然一样正常。

然后我就疑惑了,在 Widnows Terminal 中运行和在 VS Code 中有什么不同吗?

最终我发现了:

  • Widnows Terminal 中,我运行的目录是 ~/projects/demo/gitbash-node-module
  • 而在 VS Code 中,默认打开的 Git Bash 所在目录是 /e/projects/demo/gitbash-node-module

这是由于,我一开始想要在目录 home:~ 中工作,然后又不想所有的项目文件全部保存在 C盘,所以在 Home 文件夹内建了个 projects 的目录软链接符号链接,如下所示:

# PowerShell
New-Item -ItemType SymbolicLink -Path ~\projects -Target E:\projects

至此,该问题找到了,是由于 nodejs 在链接的路径中,无法找到对应的模块。

硬链接 New-Item -ItemType HardLink -Path XXX -Target XXX 应该没问题,但是硬链接只能链接文件,无法链接目录。

所以我们只能更改一下项目路径,不要用链接的路径,就可以正常开发了。

作者:  wtto
发表时间: 2023年4月1日
最后更新时间:  2024年1月1日
版权说明:  CC BY-NC-ND 4.0 DEED