Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a directory in user home using nodejs?

Tags:

node.js

I am developing a node.js application on Ubuntu and am trying to programmatically create a directory for my application in the user's home directory.

When I execute the following Javascript in Node:

const fs = require("fs");
fs.mkdirSync("~/mynewdir");

I get the following error:

Error: ENOENT: no such file or directory, mkdir '~/mynewdir'
    at Error (native)
    at Object.fs.mkdirSync (fs.js:923:18)
    at repl:1:4
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:346:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:545:10)

Executing:

fs.mkdirSync("/home/dah/mynewdir");

works just fine however, but I want to use the home directory of whomever is executing the script.

Does anyone have any suggestions?

Edit - this question is not a duplicate. In this case, the issue is not finding the home directory (I already have this), but why the fs module won't use it.

like image 790
java-addict301 Avatar asked May 22 '17 15:05

java-addict301


People also ask

How do I create a directory in node JS?

mkdir() method or Node. js fs. mkdirSync() method method, to create new directory /parent directory.

Which method is used to create a directory in node JS?

mkdir() method i Node. js is used to create a directory asynchronously.

How to get the current user home directory in Node JS?

To get the current user home directory, you can use the homedir () method from the os module in Node.js. The method returns the path to the directory as a string. See the above code live in repl.it.

How to create a directory using NodeJS?

- GeeksforGeeks How to create a directory using Node.js ? In this article, we will create a directory using NodeJS. NodeJS has Filesystem (fs) core module, which enables interacting with the file system, has Node.js fs.mkdir () method or Node.js fs.mkdirSync () method method, to create new directory /parent directory.

How to delete a folder in Node JS?

Removing a folder: If we want to delete a given directory, we can use Node.js fs.rmdir () Method or Node.js fs.rmdirSync () Method, it will become complicated if the directory contain some file content. So we can use a third party package fs-extra provided by npm to delete the given directory. Let’s install the given package using npm.

How to create home directory for an existing user in Linux?

You could easily create home directory for an existing user in Linux using mkhomedir_helper command, without deleting and recreating the user. The mkhomedir_helper is a helper program for the pam_mkhomedir module. The pam_mkhomedir PAM module will create a user's home directory if it does not exist when the session begins.


1 Answers

You can do it like that:

const homedir = require('os').homedir();
// `homedir()` returns absolute path so we use `join` here
require("fs").mkdir(require('path').join(homedir, 'mynewdir'));
like image 54
Max Koretskyi Avatar answered Oct 05 '22 01:10

Max Koretskyi