Nodejs filesystem: Difference between revisions

From wikinotes
Line 2: Line 2:
= Paths =
= Paths =
<blockquote>
<blockquote>
See https://nodejs.org/api/path.html
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
const path = require('path');
const path = require('path');

Revision as of 17:21, 30 July 2021

Paths

See https://nodejs.org/api/path.html

const path = require('path');

const filepath = path.resolve('foo.txt')  // relative to abspath
path.dirname(filepath)                    // '/home/you'
path.basename(filepath)                   // 'foo.txt'
path.extname(filepath)                    // '.txt'

// nodejs does not expand ~
// so replace it with the envvar
'~/.zshrc'.replace('~', process.env.HOME)