How to create a directory in the current directory in Node.js

James Xia

I am new to Node.js, so I'm not familiar with a lot of stuff. So basically I want to create a directory in the current working directory:

 var mkdirp = require('mkdirp');
 console.log("Going to create directory /tmp/test");
 mkdirp('/tmp/test',function(err){
       if (err) {
           return console.error(err);
        }
console.log("Directory created successfully!");
});

My current directory is C:\Users\Owner\Desktop\Tutorials\NodeJS on Windows, which means I run node main.js in that directory. (main.js is in C:\Users\Owner\Desktop\Tutorials\NodeJS) After I run the code, it generates C:\tmp\test, which is in C:\. But I want to create it in the current directory, so the result I want is C:\Users\Owner\Desktop\Tutorials\NodeJS\tmp\test.

I just don't know how to do that...

p4sh4

You can use process.cwd() to output the directory where your command has been executed (in your case, the directory where you run node main.js) so your code might look like this:

var mkdirp = require('mkdirp');
var path = require('path');

console.log("Going to create directory /tmp/test");
mkdirp(path.join(process.cwd(), '/tmp/test'), function(err){
  if (err) {
    return console.error(err);
  }
  console.log("Directory created successfully!");
});

If you need just the directory where the main.js file is located and not where you execute it (by calling node main.js), you can use the __dirname variable instead of process.cwd().

It's a good idea to use the path.join() function to make sure the path delimiters are set correctly, especially when you're on a Windows system which may treat forward slashes as options.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set the current working directory for a request in Node.js?

From Java

How to create a directory if it doesn't exist using Node.js?

From Dev

Change current directory with node

From Dev

How to create a new window on the current directory in tmux?

From Dev

node.js create hidden directory (Windows)

From Dev

How to get the current directory name and display it on the html page using node.js

From Dev

How to delete multiple PDF files in the current directory in javascript/node.js

From Dev

Is There a Command In Node.js to See My Current Directory?

From Dev

Node.js moving contents of subdirectory into current directory

From Dev

Change node path to current directory

From Dev

how to open current directory

From Dev

How to Change Default Directory in Node.JS

From Dev

How to create a text file in my current directory with NLog?

From Dev

How to create a new Image() with an image file located in a directory outside the current working directory

From Dev

Can't create a directory in node

From Dev

How to create a directory in plunker?

From Dev

How to create the hooks directory

From Java

How to set the current working directory?

From Dev

How to set current directory in GHCi?

From Dev

How to change the current working directory

From Dev

How to open terminal in current directory?

From Dev

How to change the current working directory?

From Dev

How to unzip files into the current directory?

From Dev

pyspark: how to show current directory?

From Dev

How to unzip files into the current directory?

From Dev

How to `rm` files in current directory and directories inside current directory

From Dev

Bash: How to set the current working directory to the current directory in a loop?

From Dev

How to get permissions to create a file/directory with node fs

From Dev

Node JS ENOENT - No such a file or a directory

Related Related

  1. 1

    How to set the current working directory for a request in Node.js?

  2. 2

    How to create a directory if it doesn't exist using Node.js?

  3. 3

    Change current directory with node

  4. 4

    How to create a new window on the current directory in tmux?

  5. 5

    node.js create hidden directory (Windows)

  6. 6

    How to get the current directory name and display it on the html page using node.js

  7. 7

    How to delete multiple PDF files in the current directory in javascript/node.js

  8. 8

    Is There a Command In Node.js to See My Current Directory?

  9. 9

    Node.js moving contents of subdirectory into current directory

  10. 10

    Change node path to current directory

  11. 11

    how to open current directory

  12. 12

    How to Change Default Directory in Node.JS

  13. 13

    How to create a text file in my current directory with NLog?

  14. 14

    How to create a new Image() with an image file located in a directory outside the current working directory

  15. 15

    Can't create a directory in node

  16. 16

    How to create a directory in plunker?

  17. 17

    How to create the hooks directory

  18. 18

    How to set the current working directory?

  19. 19

    How to set current directory in GHCi?

  20. 20

    How to change the current working directory

  21. 21

    How to open terminal in current directory?

  22. 22

    How to change the current working directory?

  23. 23

    How to unzip files into the current directory?

  24. 24

    pyspark: how to show current directory?

  25. 25

    How to unzip files into the current directory?

  26. 26

    How to `rm` files in current directory and directories inside current directory

  27. 27

    Bash: How to set the current working directory to the current directory in a loop?

  28. 28

    How to get permissions to create a file/directory with node fs

  29. 29

    Node JS ENOENT - No such a file or a directory

HotTag

Archive