Nodachisoft Nodachisoft logo, Katana Sword Icon
  
@Amaji✎ Update December-13-2021

How to get current directory in javascript, running on nodejs

How to get current directory in javascript, running on nodejs

Summury Of Article

This is a reminder of how to get the current directory in javascript running on nodejs, with sample code that can be copied and pasted.

There are two main ways to reference the current directory in nodejs.

Way Returning Content
__dirname Current path of the js file being executed.
process.cwd() Current working directory at runtime.

Example Codes

The sample codes are below:

 
sample.js
console.log("running now on : " + __dirname);
console.log("exec from : " + process.cwd());

Try to run this in a shell (command prompt) from a different path. The execution result is as follows:

 
Result
> pwd
/home/amaji/   <-- Check Current Working Directory!
> node /home/sample/sample.js
running now on : /home/sample/
exec from : /home/amaji/

That's all! That was from my memo!

Websites and references that were very helpful.

Page History

date modification
none
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 5 articles that may be relevant!

How to pass arguments to npm run script

How to pass arguments to npm run script

#Nodejs#Javascript✎ 2021-12-16
Describe how to pass argument to npm run script.
Table Of Contents
How to get current directory in javascript, running on nodejs
How to get current directory in javascript, running on nodejs
Summury Of Article
Summury Of Article
Example Codes
Example Codes
Websites and references that were very helpful.
Websites and references that were very helpful.
Page History
Page History
Nodachisoft © 2021