Nodejs filesystem: Difference between revisions

From wikinotes
(Created page with " = Paths = <blockquote> <syntaxhighlight lang="javascript"> const path = require('path'); const rcfile = path.resolve('foo.txt') // relative to abspath path.dirname(rcfile)...")
 
Line 5: Line 5:
const path = require('path');
const path = require('path');


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


// nodejs does not expand ~
// nodejs does not expand ~

Revision as of 17:21, 30 July 2021

Paths

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)