node.js 运行 Node 脚本时更改当前 shell 上下文中的工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19803748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change working directory in my current shell context when running Node script
提问by Jonovono
I am trying to change the working directory of my Node.js script when it is run from a bin script. I have something like the following:
当我的 Node.js 脚本从 bin 脚本运行时,我试图更改它的工作目录。我有如下内容:
#!/usr/bin/env node
process.chdir('/Users')
When I then run this file with ./bin/nodefile, it exits, but the working directory of the current shell context has not changed. I have also tried shelljs, but that does not work either.
当我用 运行这个文件时./bin/nodefile,它退出,但当前 shell 上下文的工作目录没有改变。我也试过shelljs,但这也不起作用。
What is the best way to do this? I understand it's working but it's just in a separate process.
做这个的最好方式是什么?我知道它正在起作用,但它只是在一个单独的过程中。
回答by hexacyanide
The correct way to change directories is actually with process.chdir(directory). Here's an example from the documentation:
更改目录的正确方法实际上是使用process.chdir(directory). 这是文档中的一个示例:
console.log('Starting directory: ' + process.cwd());
try {
process.chdir('/tmp');
console.log('New directory: ' + process.cwd());
}
catch (err) {
console.log('chdir: ' + err);
}
This is also testable in the Node.js REPL:
这也可以在 Node.js REPL 中测试:
[monitor@s2 ~]$ node
> process.cwd()
'/home/monitor'
> process.chdir('../');
undefined
> process.cwd();
'/home'
回答by dthree
There is no built-in method for Node to change the CWD of the underlying shellrunning the Node process.
Node 没有内置方法来更改运行 Node 进程的底层 shell的 CWD 。
You canchange the current working directory of the Node processthrough the command process.chdir().
您可以通过命令更改Node 进程的当前工作目录process.chdir()。
var process = require('process');
process.chdir('../');
When the Node process exists, you will find yourself back in the CWD you started the process in.
当 Node 进程存在时,你会发现自己回到了启动进程的 CWD。
回答by Spike Grobstein
What you are trying to do is not possible. The reason for this is that in a POSIX system (Linux, OSX, etc), a child process cannot modify the environment of a parent process. This includes modifying the parent process's working directory and environment variables.
你试图做的事情是不可能的。这样做的原因是在 POSIX 系统(Linux、OSX 等)中,子进程无法修改父进程的环境。这包括修改父进程的工作目录和环境变量。
When you are on the commandline and you go to execute your Node script, your current process (bash, zsh, whatever) spawns a new process which has it's own environment, typically a copy of your current environment (it is possible to change this via system calls; but that's beyond the scope of this reply), allowing that process to do whatever it needs to do in complete isolation. When the subprocess exits, control is handed back to your shell's process, where the environment hasn't been affected.
当你在命令行上执行你的 Node 脚本时,你当前的进程(bash,,zsh等等)会产生一个新进程,它有自己的环境,通常是你当前环境的副本(可以通过系统调用来改变这个; 但这超出了本回复的范围),允许该进程在完全隔离的情况下做它需要做的任何事情。当子进程退出时,控制权被交还给你的 shell 进程,在那里环境没有受到影响。
There are a lot of reasons for this, but for one, imagine that you executed a script in the background (via ./foo.js &) and as it ran, it started changing your working directory or overriding your PATH. That would be a nightmare.
造成这种情况的原因有很多,但首先,假设您在后台(通过./foo.js &)执行了一个脚本,并且在它运行时,它开始更改您的工作目录或覆盖您的PATH. 那将是一场噩梦。
If you need to perform some actions that require changing your working directory of your shell, you'll need to write a function in your shell. For example, if you're running Bash, you could put this in your ~/.bash_profile:
如果您需要执行一些需要更改 shell 工作目录的操作,则需要在 shell 中编写一个函数。例如,如果你正在运行 Bash,你可以把它放在你的~/.bash_profile:
do_cool_thing() {
cd "/Users"
echo "Hey, I'm in $PWD"
}
and then this cool thing is doable:
然后这个很酷的事情是可行的:
$ pwd
/Users/spike
$ do_cool_thing
Hey, I'm in /Users
$ pwd
/Users
If you need to do more complex things in addition, you could always call out to your nodejs script from that function.
如果您还需要做更复杂的事情,您可以随时从该函数调用您的 nodejs 脚本。
This is the only way you can accomplish what you're trying to do.
这是您可以完成您正在尝试做的事情的唯一方法。
回答by Evolopment
Short answer: no (easy?) way, but you can do something that serves your purpose.
简短回答:没有(简单?)方式,但您可以做一些符合您目的的事情。
I've done a similar tool (a small command that, given a description of a project, sets environment, paths, directories, etc.). What I do is set-up everything and then spawn a shell with:
我做了一个类似的工具(一个小命令,给出一个项目的描述,设置环境、路径、目录等)。我所做的是设置所有内容,然后使用以下命令生成一个 shell:
spawn('bash', ['-i'], {
cwd: new_cwd,
env: new_env,
stdio: 'inherit'
});
After execution, you'll be on a shell with the new directory (and, in my case, environment). Of course you can change bash for whatever shell you prefer. The main differences with what you originally asked for are:
执行后,您将使用新目录(在我的情况下为环境)的 shell。当然,您可以为您喜欢的任何 shell 更改 bash。与您最初要求的主要区别是:
- There is an additional process, so...
- you have to write 'exit' to come back, and then...
- after existing, all changes are undone.
- 还有一个额外的过程,所以......
- 你必须写'exit'回来,然后......
- 存在后,所有更改都将撤消。
However, for me, that differences are desirable.
然而,对我来说,这种差异是可取的。

