Reputation: 935
I often see examples in node like:
process.env.GOOGLE_APPLICATION_CREDENTIALS = path.resolve(__dirname, 'credentials.json')
I understand that path
is a useful module when dealing with more complex situations, but in this case, why not just use
process.env.GOOGLE_APPLICATION_CREDENTIALS = './credentials.json'
?
Upvotes: 1
Views: 1190
Reputation: 575
From the Documentation:
The directory name of the current module. This is the same as the path.dirname() of the __filename.
This means when using this:
process.env.GOOGLE_APPLICATION_CREDENTIALS = './credentials.json'
NodeJS will look for credentials.json
in the current working directory
While when using this:
process.env.GOOGLE_APPLICATION_CREDENTIALS = path.resolve(__dirname, 'credentials.json')
NodeJS will look for credentials.json
in the same directory where this module is
Lets say you have a directory structure like this:
/
+-- home
| +-- USERNAME
| +-- project
| +-- index.js
| +-- credentials.json
if you run the following commands (from the root directory /
):
cd home/USERNAME/project
node index.js
The code will work just fine in both cases.
But if you run it like this
node home/USERNAME/project/index.js
With path.resolve
you'll get: home/USERNAME/project/credentials.json
which is correct
and without it you'll get: ./credentials.json
, which is wrong as your current directory is /
.
Upvotes: 3