如何在 Windows 上为 Node.js 设置工作目录? [英] How to set working directory for Node.js on windows?

查看:57
本文介绍了如何在 Windows 上为 Node.js 设置工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

//设置var FILE_ENCODING = 'utf-8',EOL = '\n',DIST_FILE_PATH = 'dist/myAwesomeScript.js';//设置var _fs = require('fs');函数连接(文件列表,distPath){var out = fileList.map(function(filePath){返回 _fs.readFileSync(filePath, FILE_ENCODING);});_fs.writeFileSync(distPath, out.join(EOL), FILE_ENCODING);console.log(' '+ distPath +' 构建.');}连接(['foo/bar.js','foo/lorem.js','foo/maecennas.js'], DIST_FILE_PATH);

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

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

更新

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

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

解决方案

您可以使用 process.chdir,使用 Unix 风格的路径名:

<前>process.chdir('/temp/foo');

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

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:

// 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.

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?

UPDATE

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

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

解决方案

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

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

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

这篇关于如何在 Windows 上为 Node.js 设置工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆