Categories
JavaScript Answers

How to get version from package.json in Node.js code?

Sometimes, we want to get version from package.json in Node.js code.

In this article, we’ll look at how to get version from package.json in Node.js code.

How to get version from package.json in Node.js code?

To get version from package.json in Node.js code, we can import the content of package.json.

For instance, we write

import {
  version
} from './package.json';

to import the version property from package.json as a string.

Conclusion

To get version from package.json in Node.js code, we can import the content of package.json.

Categories
JavaScript Answers

How to run a command line binary with Node.js?

Sometimes, we want to run a command line binary with Node.js.

In this article, we’ll look at how to run a command line binary with Node.js.

How to run a command line binary with Node.js?

To run a command line binary with Node.js, we can use the child_process module.

For instance, we write

const {
  exec
} = require('child_process');

exec('cat *.js bad_file | wc -l', (err, stdout, stderr) => {
  if (err) {
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

to call exec with the command we want to run and the callback that runs when the command is done.

In the callback we return the standout output content with stdout.

And we return the standard error output with stderr.

Conclusion

To run a command line binary with Node.js, we can use the child_process module.

Categories
JavaScript Answers

How to read a JSON file into server memory with Node.js?

Sometimes, we want to read a JSON file into server memory with Node.js.

In this article, we’ll look at how to read a JSON file into server memory with Node.js.

How to read a JSON file into server memory with Node.js?

To read a JSON file into server memory with Node.js, we can use the readFile or readFileSync method.

For instance, we write

const fs = require('fs');
fs.readFile('file', 'utf8', (err, data) => {
  if (err) {
    throw err;
  }
  const obj = JSON.parse(data);
});

to call fs.readFile with the file path, encoding of the file we’re reading, and a callback that runs when the method is done running.

If there’s an error, err is set and we throw it.

Otherwise, we call JSON.parse with the file data we read.

readFile reads the file asynchronously.

We can read JSON files synchronously with readFileSync.

To use it, we write

const fs = require('fs');
const obj = JSON.parse(fs.readFileSync('file', 'utf8'));

to call it with the file path and encoding and return the file directly.

And then we call JSON.parse on the returned string file contents.

Conclusion

To read a JSON file into server memory with Node.js, we can use the readFile or readFileSync method.

Categories
JavaScript Answers

How to create a quick file server with Node.js to server static files via HTTP?

Sometimes, we want to create a quick file server with Node.js to server static files via HTTP.

In this article, we’ll look at how to create a quick file server with Node.js to server static files via HTTP.

How to create a quick file server with Node.js to server static files via HTTP?

To create a quick file server with Node.js to server static files via HTTP, we can use the http, final-handler and serve-static packages.

For instance, we write

const http = require('http');
const finalhandler = require('finalhandler');
const serveStatic = require('serve-static');

const serve = serveStatic("./");

const server = http.createServer((req, res) => {
  const done = finalhandler(req, res);
  serve(req, res, done);
});

to call serveStatic with the path of the folder we want to serve files from.

Then we create the HTTP server with http.createServer with a callback that calls finalhandler to return the done function which runs when the request processing is done.

And then we call serve with the request req object, response res object, and the done function.

Conclusion

To create a quick file server with Node.js to server static files via HTTP, we can use the http, final-handler and serve-static packages.

Categories
JavaScript Answers

How to change a Node.js’s console font color?

Sometimes, we want to change a Node.js’s console font color.

In this article, we’ll look at how to change a Node.js’s console font color.

How to change a Node.js’s console font color?

To change a Node.js’s console font color, we can call console.log with the color code we want for the text.

For instance, we write

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow); 

to print cyan and yellow text respectivelt.

The first string argument is the color code.

The list of color codes and text styles include

Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"

Foreground Black = "\x1b[30m"
Foreground Red = "\x1b[31m"
Foreground Green = "\x1b[32m"
Foreground Yellow = "\x1b[33m"
Foreground Blue = "\x1b[34m"
Foreground Magenta = "\x1b[35m"
Foreground Cyan = "\x1b[36m"
Foreground White = "\x1b[37m"

Background Black = "\x1b[40m"
Background Red = "\x1b[41m"
Background Green = "\x1b[42m"
Background Yellow = "\x1b[43m"
Background Blue = "\x1b[44m"
Background Magenta = "\x1b[45m"
Background Cyan = "\x1b[46m"
Background White = "\x1b[47m"

Conclusion

To change a Node.js’s console font color, we can call console.log with the color code we want for the text.