gpt4 book ai didi

node.js - 生成 Node 子进程时不遵守 cwd

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:58 24 4
gpt4 key购买 nike

我正在尝试使用 Node v11.1.0 生成一个同步子进程并设置当前工作目录如下。我使用的是 Windows 10。

const options = {
cwd: 'D:\\somepath\\node_modules\\.install-npm-version-temp-U9EUNd',
stdio: 'inherit'
};
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
childProcess.spawnSync(command, ['install', 'chalk@2.4.1'], options);

本例中父进程当前工作目录为'D:\\somepath'

子进程执行完毕后,我期望 chalk@2.4.1 会安装到上面设置的 cwd 中,但事实并非如此。相反,它被安装在 'D:\\somepath\\node_modules' 中,这使我相信子进程的当前工作目录实际上是父进程的当前工作目录。表明这一点的进一步证据是,由于子进程运行以包含依赖项 chalk@2.4.1'D:\\somepath\\package.json' 被修改>(之前不存在)。

注意,上面的cwd路径在我的机器上有效。 NPM 命令执行并报告成功。

基于Node docs ,我相信我正确使用了spawnSync API,但我没有运气调试这个。有什么建议吗?

最佳答案

您在 D:\somepath 中有一个 package.json 文件,而不是在 cwd 中。

来自npm docs :

When installing locally, npm first tries to find an appropriate prefix folder. This is so that npm install foo@1.2.3 will install to the sensible root of your package, even if you happen to have cded into some other folder.

Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)

If no package root is found, then the current folder is used.

关于node.js - 生成 Node 子进程时不遵守 cwd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53221994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com