Process.env.PWD vs process.cwd ()

I use Meteor JS ... and in my Meteor application I use node to request the contents of different directories in the application ....

When I use process.env.PWD to query the contents of a folder, I get a different result when I use process.cwd () to query the results in 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' ] 

using process.cwd () only shows "server.js" inside the Meteor.

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

+5
source share
1 answer

They are related to each other, but not the same thing.

process.env.PWD is the working directory when the process starts. This remains unchanged for the entire process.

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

You can manipulate PWD , but it would be pointless to do this, this variable is not used by anything, it is just for convenience.

To calculate the paths, you probably want to do this as follows:

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

Where __dirname reflects the directory in which the source file is located, this code is listed in the residence. It is wrong to expect cwd() be anywhere. If your server process is running from anywhere except the main source directory, all your paths will be invalid using cwd() .

+8
source

All Articles