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'
?
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 /
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.