如何在 Windows 上为 Node.js 设置工作目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9956316/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:25:11  来源:igfitidea点击:

How to set working directory for Node.js on windows?

windowsnode.js

提问by Christoph

I just installed node.js for windows and it really was a breeze to get it running. I would like to use it as part of my build process to combine several files together like so:

我刚刚为 Windows 安装了 node.js,让它运行起来真的很容易。我想将它用作构建过程的一部分,将多个文件组合在一起,如下所示:

// settings
var FILE_ENCODING = 'utf-8',
    EOL = '\n',
    DIST_FILE_PATH = 'dist/myAwesomeScript.js';

// setup
var _fs = require('fs');

function concat(fileList, distPath) {
    var out = fileList.map(function(filePath){
            return _fs.readFileSync(filePath, FILE_ENCODING);
        });
    _fs.writeFileSync(distPath, out.join(EOL), FILE_ENCODING);
    console.log(' '+ distPath +' built.');
}

concat([
    'foo/bar.js',
    'foo/lorem.js',
    'foo/maecennas.js'
], DIST_FILE_PATH);

This really works like a charm. However it does only work if I place all my scripts into the nodejs directory which is C:\Program Files (x86)\nodejs and start the cmd process with admin rights.

这真的很有魅力。但是,只有将所有脚本放入 nodejs 目录 C:\Program Files (x86)\nodejs 并以管理员权限启动 cmd 进程时,它才有效。

But I need to have my project files in another directory ( say D:\git\projectx\ ) and would like to be able to run: node.exe D:\git\projectx\combine.js. Unfortunatly things doesn't work that way because node.exe will look for the files within it's own directory which is C:\Program Files (x86)\nodejs. There must be away to start the nodejs process and tell it to use another directory as its working directory, am I wrong?

但是我需要将我的项目文件放在另一个目录中(比如 D:\git\projectx\)并且希望能够运行:node.exe D:\git\projectx\combine.js。不幸的是,事情不会那样工作,因为 node.exe 将在它自己的目录中查找文件,即 C:\Program Files (x86)\nodejs。必须启动nodejs进程并告诉它使用另一个目录作为其工作目录,我错了吗?

UPDATE

更新

As someone pointed out on IRC. The solution to my problem was rather simple. Just cdinto D:\git\projectxand then use node.exe combine.js. This makes it so that the current directory inside your script points to D:\git\projectx

正如有人在 IRC 上指出的那样。我的问题的解决方案相当简单。只需cd进入D:\git\projectx然后使用node.exe combine.js. 这使得脚本中的当前目录指向D:\git\projectx

However, I'm accepting Luke's answer since it seems to be also true ;-)

但是,我接受卢克的回答,因为它似乎也是正确的 ;-)

回答by Luke Girvin

You can set the current working directory using process.chdir, using Unix-style pathnames:

您可以使用process.chdir设置当前工作目录,使用 Unix 样式的路径名:

process.chdir('/temp/foo');

I'm not sure how to specify the drive prefix (D:) though.

不过,我不确定如何指定驱动器前缀 ( D:)。

回答by Tracker1

You can always use __dirnameto represent the directory of the script you are in...

您始终可以使用__dirname来表示您所在脚本的目录...

process.chdir(__dirname);

回答by Sooraj P S

process.chdir('D:\git\projectx')

回答by Vincent J

Actually it's

其实是

process.chdir('D:\\git\projectx')