process.env.PWD vs process.cwd() [英] process.env.PWD vs process.cwd()

查看:134
本文介绍了process.env.PWD vs process.cwd()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Meteor JS ...并且在我的Meteor应用程序中我使用节点查询应用程序内不同目录的内容....

I am using Meteor JS...and within my Meteor app I am using node to query the contents of different directories within the app....

我使用process.env.PWD查询文件夹的内容,当我使用process.cwd()查询文件夹的结果时,得到的结果不同。

When I use process.env.PWD to query the contents of a folder I get a different result from when I use process.cwd() to query the results of a folder.

var dirServer = process.env.PWD + '/server/';
var dirServerFiles = fs.readdirSync(dirServer);
console.log(dirServerFiles); //outputs: [ 'ephe', 'fixstars.cat', 'sepl_30.se1', 'server.js' ]

vs

var serverFolderFilesDir = process.cwd() +"/app/server";
var serverFolderFiles = fs.readdirSync(serverFolderFilesDir);
console.log(serverFolderFiles); //outputs: [ 'server.js' ]

使用process.cwd()只显示'服务器流星中的.js'。

using process.cwd() only shows 'server.js' within the Meteor.

这是为什么?
process.cwd()与process.env.PWD有什么不同?

Why is this? How is process.cwd() different from process.env.PWD?

推荐答案

它们是相关但不是同样的事情。

They're related but not the same thing.

process.env.PWD 进程启动时的工作目录的。整个过程保持不变。

process.env.PWD is the working directory when the process was started. This stays the same for the entire process.

process.cwd()当前工作目录。它反映了通过 process.chdir()所做的更改。

process.cwd() is the current working directory. It reflects changes made via process.chdir().

可以操作 PWD 但这样做没有意义,任何东西都不会使用该变量,只是为了方便起见。

It's possible to manipulate PWD but doing so would be meaningless, that variable isn't used by anything, it's just there for convenience.

对于你可能想要的计算路径这样做:

For computing paths you probably want to do it this way:

var path = require('path');
path.resolve(__dirname, 'app/server')

其中 __ dirname 反映了此代码在驻留中定义的源文件的目录。期望 cwd()将接近该位置是错误的。如果你的服务器进程是从任何地方启动的,但主源目录的所有路径都是错误的,使用 cwd()

Where __dirname reflects the directory the source file this code is defined in resides. It's wrong to expect that cwd() will be anywhere near that. If your server process is launched from anywhere but the main source directory all your paths will be incorrect using cwd().

这篇关于process.env.PWD vs process.cwd()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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