Categories
JavaScript Answers

How to fix ‘ReferenceError: fetch is not defined’ error in Node.js?

Sometimes, we want to fix ‘ReferenceError: fetch is not defined’ error in Node.js.

In this article, we’ll look at how to fix ‘ReferenceError: fetch is not defined’ error in Node.js.

How to fix ‘ReferenceError: fetch is not defined’ error in Node.js?

To fix ‘ReferenceError: fetch is not defined’ error in Node.js, we can install node-fetch.

To install it, we run

npm i node-fetch

Then we import it with

import fetch from "node-fetch";

so we can call fetch.

Conclusion

To fix ‘ReferenceError: fetch is not defined’ error in Node.js, we can install node-fetch.

Categories
JavaScript Answers

How to measure the execution time of Node.js code with callbacks?

Sometimes, we want to measure the execution time of Node.js code with callbacks.

In this article, we’ll look at how to measure the execution time of Node.js code with callbacks.

How to measure the execution time of Node.js code with callbacks?

To measure the execution time of Node.js code with callbacks, we can use the process.hrtime method.

For instance, we write

let start = process.hrtime();

const elapsedTime = (note) => {
  const precision = 3;
  const [startTime, endTime] = process.hrtime(start)
  const elapsed = endTime / 1000000;
  console.log(startTime, elapsed);
  start = process.hrtime();
}

to create the elaspedTime function tyat calls process.hrtime with start to return the timestamp of the elasped time and assign that to endTime.

The endTime is in nanoseconds.

So we divide endTime by 1000000 to get the elasped time in seconds.

Conclusion

To measure the execution time of Node.js code with callbacks, we can use the process.hrtime method.

Categories
JavaScript Answers

How to create a hash string with Node.js?

Sometimes, we want to create a hash string with Node.js.

In this article, we’ll look at how to create a hash string with Node.js.

How to create a hash string with Node.js?

To create a hash string with Node.js, we can use the crypto module.

For instance, we write

const crypto = require('crypto');
const name = 'hello';
const hash = crypto.createHash('md5').update(name).digest('hex');

to call crypto.createHash with 'md5' to create a new hash string.

And we call update to create the hash string hashed from name.

Finally, we call digest with 'hex' to return the hashed string as hex.

Conclusion

To create a hash string with Node.js, we can use the crypto module.

Categories
JavaScript Answers

How to set environment variables from within package.json?

Sometimes, we want to set environment variables from within package.json.

In this article, we’ll look at how to set environment variables from within package.json.

How to set environment variables from within package.json?

To set environment variables from within package.json, we can put the keys and values of the environment variables before the command we run.

For instance, we write

...
"scripts": {
  "start": "node app.js",
  "test": "NODE_ENV=test mocha --reporter spec"
},
...

to add the test script that sets the NODE_ENV environment variable to test.

And then we run the mocha command with the environment variable set.

Conclusion

To set environment variables from within package.json, we can put the keys and values of the environment variables before the command we run.

Categories
JavaScript Answers

How to pretty-print JSON using Node.js?

Sometimes, we want to pretty-print JSON using Node.js.

In this article, we’ll look at how to pretty-print JSON using Node.js.

How to pretty-print JSON using Node.js?

To pretty-print JSON using Node.js, we can call JSON.stringify with the number of spaces to indent the JSON by.

For instance, we write

const fs = require('fs');

fs.writeFile('test.json', JSON.stringify({
  a: 1,
  b: 2,
  c: 3
}, null, 2));

to call fs.writeFile with the file path and the stringified JSON.

We stringify the JSON by calling JSON.stringify with the object to stringify as the first argument and the number of spaces to indent each level by as the 3rd argument.

As a result, the JSON written to test.json should be indented with 2 spaces in each level.

Conclusion

To pretty-print JSON using Node.js, we can call JSON.stringify with the number of spaces to indent the JSON by.