Commands node -v node version Exports & require module.exports = obj
const gtr = {
brand: 'nissan',
model: 'gtr'
}
module.exports = gtr
require('./car')
exports.prop = obj
const fiesta = {
brand: 'ford',
model: 'fiesta'
}
const golf = {
brand: 'vw',
model: 'golf'
}
exports.fiesta = fiesta
exports.golf = golf
// same as
// module.exports = { fiesta, golf }
require('./cars')
require('./cars').fiesta
require('./cars').golf
module.exports = { obj1, obj2 }
const model3 = {
brand: 'tesla',
model: 'model3'
}
const m5 = {
brand: 'bmw',
model: 'm5'
}
module.exports = { model3, m5 }
require('./cars')
require('./cars').m5
require('./cars').model3
Pass args to a script we may trigger a script like node server.js but how to pass arguments into that we can do node server.js one two=three four ad get args with process.argv
// $ node server.js one two=three four
const args = process.argv
console.log(args)
// ['node', '/home/server.js', 'one', 'two=three', 'four']
Path
const thisFilePath = fileURLToPath(import.meta.url)
const thisDirName = dirname(thisFilePath)
const filePathInSameFolder = join(thisDirName, 'readme.txt')
path.join('folder', 'subfolder', 'file.txt') // 'folder/subfolder/file.txt'
path.join('/folder', 'subfolder', 'file.txt') // '/folder/subfolder/file.txt'
path.join('folder', '/subfolder', 'file.txt') // 'folder/subfolder/file.txt'
path.resolve('folder', 'subfolder', 'file.txt') // '/current/working/directory/folder/subfolder/file.txt'
path.resolve('/folder', 'subfolder', 'file.txt') // '/folder/subfolder/file.txt'
path.resolve('folder', '/subfolder', 'file.txt') // '/subfolder/file.txt'