folder 01
// in index.js
console.log(arguments)This prints out the arguments huge object to the console
[Arguments] {
...
}// in index.js
console.log(arguments)
console.log(require)
console.log(module)
console.log(__filename)
console.log(__dirname)// in index.js
require('./myscript.js')
console.log(require.cache)
// in myscript.js
const message = 'hello world'
module.exports = message// in index.js
const counterObject = require('./myscript.js')
console.log(counterObject.getCounter())
counterObject.incrementCounter()
console.table(counterObject.getCounter())
const newCounterObject = require('./myscript.js')
console.log(newCounterObject.getCounter())
// in myscript.js
let counter = 0
module.exports = {
incrementCounter() {
counter = counter + 1
},
getCounter() {
return counter
},
}node inspect index.js
- c => Continue execution until program ends or next debugger statement
- n => Run the next line of code
- s => Step in to a function
- o => Step out of the current function
After entering the debu> we can open the repl console by typing repl in the debugger
node inspect index.js => Start up a debugger CLI and pause execution whenever a 'debugger' statement is hit
node --inspect index.js => Start up a debugger instance and pause execution whenever a 'debugger' statement is hit. Access the debugger at 'chrome://inspect'
node --inspect-brk index.js => Start up a debugger instance and wait to execute until a debugger is connected. Access the debugger at 'chrome://inspect'
folder 01
NodeJS documentation https://nodejs.org/
https://nodejs.org/dist/latest-v18.x/docs/api/fs.html
const fs = require('fs')
fs.readdir('.', (err, filenames) => {
// EITHER
// err === an error object, which means something went wrong
// OR
// err === null, which means everything is OK
if (err) {
// error handling code here
console.log(err)
// return
}
console.log(filenames)
})node index.js
node 02-list/index.js
npm init -y
Change index.js file permissions
chmod +x index.js
Allow us to run it like an executable
#!/usr/bin/env node
Takes our current project and makes it available to everywhere or every directory on our machine
npm link
Run our program
nls
which node
// BAD CODE HERE!!!!
for (let filename of filenames) {
fs.lstat(filename, (err, stats) => {
if (err) {
console.log(err)
}
console.log(filename, stats.isFile())
})
}const fs = require('fs')
fs.readdir(process.cwd(), (err, filenames) => {
if (err) {
console.log(err)
}
// console.log(filenames)
// Creates an array with a length equal to the filenames array
// and every value or index will be filled with the value of null
const allStats = Array(filenames.length).fill(null)
for (let filename of filenames) {
const index = filenames.indexOf(filename)
fs.lstat(filename, (err, stats) => {
if (err) {
console.log(err)
}
allStats[index] = stats
// every() func is built into every single array
const ready = allStats.every((stats) => {
return stats
})
if (ready) {
allStats.forEach((stats, index) => {
// console.log(filename);
console.log(filenames[index], stats.isFile())
})
}
})
}
})const fs = require('fs')
const util = require('util')
// Method #2
// const lstat = util.promisify(fs.lstat)
// Method #3
const { lstat } = fs.promises
fs.readdir(process.cwd(), (err, filenames) => {
if (err) {
console.log(err)
}
})
// Method #1
// const lstat = (filename) => {
// return new Promise((resolve, reject) => {
// fs.lstat(filename, (err, stats) => {
// if(err) {
// reject(err)
// }
// resolve(stats)
// })
// })
// }fs.readdir(process.cwd(), async (err, filenames) => {
if (err) {
console.log(err)
}
for (let filename of filenames) {
try {
const stats = await lstat(filename)
console.log(filename, stats.isFile())
} catch (err) {
console.log(err)
}
}
})