Git bash 或 Windows 上的 NPM 正在将参数转换为路径

Git bash or NPM on Windows is converting an argument into a path

我正在编写 node.js 脚本来修补服务器文件。我们的开发机器是 Windows,但服务器是 Linux。

当我执行 运行 以下脚本时:

npm run patch-file --source-file "/path/to/file" --destination "/path/to/file"

在 Git bash 或 NPM 之间的某处,参数被转换为 windows 路径。我不想在这里发生。我正在根据脚本本身的源/目标处理路径转换。

实际输出:

node ./scripts/patch-file.js "--source-file" "C:/Git/path/to/file" "--destination" "C:/Git/path/to/file"

预期输出:

node ./scripts/patch-file.js "--source-file" "/path/to/file" "--destination" "/path/to/file"

备注

这似乎与 Git bash 隔离,我无法使用 windows 命令提示符或 powershell 复制它。我仍然想解决这个问题,因为我希望它能在命令终端之间无缝工作。

我相信您正在寻找环境变量 MSYS_NO_PATHCONV=1,正如所讨论的 here or here

我用 notepad.exe 做了一个测试。虽然这有效并从 Git 的根目录打开了用于 Windows 安装的文本文件

notepad.exe /LICENSE.txt

建议进行路径转换,但失败,notepad.exe 抱怨找不到文件 /LICENSE.txt

MSYS_NO_PATHCONV=1 notepad.exe /LICENSE.txt

所以,我认为后一种形式是你需要的。

请注意,对于 Windows,MSYS_NO_PATHCONV 似乎确实特定于 Git,并且在其他基于 MSYS2 的终端中不可用。