Tagged: nodejs

0

How to read a file as a string in nodejs javascript?

Below code reads the file con­tent in asyn­chro­nous man­ner and the con­tent is returned in the string for­mat and buffered for­mat respec­tively. let fs = require(“fs-extra”); let path = require(“path”); fs.readFile(path.join(__dirname, “filePath”), “utf8”,(data, err)…

0

How to run cli/unix commands from nodejs scripts?

There are two ways to do this: child_process.exec(command[, options][, call­back]) // ls.js const { exec } = require(‘child_process’); exec(‘ls –lh /usr’, (error, std­out, stderr) => { if (error) { console.error(‘exec error: ${error}‘); return; } console.log(‘stdout:…

0

path.join in nodejs

path.join() does two things : Joins all given path seg­ments together using the plat­form spe­cific sep­a­ra­tor as a delim­iter Nor­mal­izes the result­ing path path.join(‘src/’, ‘js/Router.jsx’); ‘src/js/Router.jsx’ on OSX and Linux ‘srcjsRouter.jsx’ on Win­dows Here, it…