Categories
JavaScript Answers

How to download and unzip files in Node.js cross-platform?

To download and unzip files in Node.js cross-platform, we use the zlib library.

For instance, we write

const zlib = require("zlib");

zlib.gunzip(gzipBuffer, (err, result) => {
  if (err) {
    return console.error(err);
  }

  console.log(result);
});

to call gunzip to unzip the gzipBuffer file.

We get the unzipped file from the result parameter in the callback.

Categories
JavaScript Answers

How to update each dependency in package.json to the latest version with Yarn and JavaScript?

To update each dependency in package.json to the latest version with Yarn and JavaScript, we run yarn upgrade.

For instance, we run

yarn upgrade <package-name> --latest

to upgrade the package with yarn upgrade to the latest version with the --latest flag.

Categories
JavaScript Answers

How to make Sequelize OR query with Node.js?

To make Sequelize OR query with Node.js, we call findAll with an object with the where property.

For instance, we write

Post.findAll({
  where: {
    authorId: {
      [Op.or]: [12, 13],
    },
  },
});

to call findAll with an object to query the Post items with authorId equal to 12 or 13, which is

SELECT * FROM post WHERE authorId = 12 OR authorId = 13;
Categories
JavaScript Answers

How to generate an MD5 file hash in JavaScript and Node.js?

To generate an MD5 file hash in JavaScript and Node.js, we use CryptoJS.

For instance, we write

import MD5 from "crypto-js/md5";
console.log(MD5("Message").toString());

to call the MD5 function to encrypt the 'Message' message.

We return the hash as a string with toString.

Categories
JavaScript Answers

How to uninstall global package with npm with JavaScript?

To uninstall global package with npm with JavaScript, we run npm uninstall with the -g flag.

For instance, we run

npm uninstall -g webpack

to uninstall the global version of webpack.