Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding / Resolving ~ in node.js

Tags:

path

node.js

I am new to nodejs. Can node resolve ~ (unix home directory) example ~foo, ~bar to /home/foo, /home/bar

 > path.normalize('~mvaidya')  '~mvaidya' > path.resolve('~mvaidya')  '/home/mvaidya/~mvaidya' >   

This response is wrong; I am hoping that ~mvaidya must resolve to /home/mvaidya

like image 495
forvaidya Avatar asked Jan 12 '14 17:01

forvaidya


People also ask

How do I increase node js memory limit?

Solution. The solution to run your Node. js app with increased memory is to start the process with an additional V8 flag: --max-old-space-size . You need to append your desired memory size in megabytes.

What is heap size in NodeJS?

By default, Node. js (up to 11. x ) uses a maximum heap size of 700MB and 1400MB on 32-bit and 64-bit platforms, respectively.

What is Sigint in node js?

SIGINT is generated by the user pressing Ctrl + C and is an interrupt. SIGTERM is a signal that is sent to request the process terminates. The kill command sends a SIGTERM and it's a terminate. You can catch both SIGTERM and SIGINT and you will always be able to close the process with a SIGKILL or kill -9 [pid] .

What is process CWD?

process. cwd() returns the current working directory, i.e. the directory from which you invoked the node command. __dirname returns the directory name of the directory containing the JavaScript source code file.


Video Answer


1 Answers

As QZ Support noted, you can use process.env.HOME on OSX/Linux. Here's a simple function with no dependencies.

const path = require('path'); function resolveHome(filepath) {     if (filepath[0] === '~') {         return path.join(process.env.HOME, filepath.slice(1));     }     return filepath; } 
like image 123
Pj Dietz Avatar answered Oct 15 '22 03:10

Pj Dietz