Categories
JavaScript Answers

How to fix bcrypt invalid elf header error when running Node app?

To fix bcrypt invalid elf header error when running Node app, we should use a bcrypt version that’s compatible with the OS the app is running.

To do this, we run

npm install bcrypt

to install bcrypt on the current operating system so that it’s built to run on the current OS.

Categories
JavaScript Answers

How to fix env: node: No such file or directory error in Mac?

To fix env: node: No such file or directory error in Mac, we reinstall Node and npm.

For instance, we run

$ brew uninstall --force node
$ brew uninstall --force npm

to uninstall node and npm with brew.

And then we reinstall both with

$ brew install node
Categories
JavaScript Answers

How to use JavaScript async/await with streams?

To use JavaScript async/await with streams, we use the util.promisify method.

For instance, we write

const fs = require("fs");
const crypto = require("crypto");
const util = require("util");
const stream = require("stream");

const pipeline = util.promisify(stream.pipeline);

const hash = crypto.createHash("sha1");
hash.setEncoding("hex");

const run = async () => {
  await pipeline(fs.createReadStream("/some/file/name.txt"), hash);
  console.log("Pipeline succeeded");
};

run();

to call util.promisify with stream.pipeline to return the promisified pipeline function.

And then we call pipeline with a read stream created from createReadStream to run the pipeline.

Categories
JavaScript Answers

How to fix error installing bcrypt with npm?

To fix error installing bcrypt with npm, we install the bcryptjs module.

For instance, we run

npm install --save bcryptjs && npm uninstall --save bcrypt

to install the bcryptjs module and uninstall the bcrypt module.

Categories
JavaScript Answers

How to get city name from a latitude and longitude point with JavaScript?

To get city name from a latitude and longitude point with JavaScript, we use the MapQuest API.

For instance, we write

const fetchLocationName = async (lat, lng) => {
  const response = await fetch(
    "https://www.mapquestapi.com/geocoding/v1/reverse?key=API-Key&location=" +
      lat +
      "%2C" +
      lng +
      "&outFormat=json&thumbMaps=false"
  );
  const responseJson = await response.json();
  console.log(JSON.stringify(responseJson));
};

to make a get request to the MapQuest reverse geocoding URL with fetch with the lat latitude and lng longitude.

Then we call json to get the JSON response body from response.