Nodejs filesystem

From wikinotes
Revision as of 17:16, 30 July 2021 by Will (talk | contribs) (Created page with " = Paths = <blockquote> <syntaxhighlight lang="javascript"> const path = require('path'); const rcfile = path.resolve('foo.txt') // relative to abspath path.dirname(rcfile)...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Paths

const path = require('path');

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

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