process.env.PWD vs process.cwd() process.env.PWD vs process.cwd() javascript javascript

process.env.PWD vs process.cwd()


They're related but not the same thing.

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

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

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')

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().