Categories
JavaScript Answers

How to fix the ‘Not compatible with your operating system or architecture: fsevents@1.0.11’ error on Node on Mac?

To fix the ‘Not compatible with your operating system or architecture: fsevents@1.0.11’ error on Node on Mac, we skip installing optional dependencies.

To do this, we run

npm install --no-optional

to run npm install with the --no-optional flag to skip installing optional dependencies.

Categories
JavaScript Answers

How to get a variable from a file to another file in Node.js?

To get a variable from a file to another file in Node.js, we use modules.

For instance, we write

module.exports = { clientIDUnsplash: "SuperSecretKey" };

to export an object with the clientIDUnsplash property in unsplash.js.

Then we write

const { clientIDUnsplash } = require("./unsplash");

to call require to require the unsplash.js file and get the clientIDUnsplash property from the returned object.

Categories
JavaScript Answers

How to generate guid/uuid in TypeScript Node.js app?

To generate guid/uuid in TypeScript Node.js app, we use the uuid module.

For instance, we write

import { v4 as uuid } from "uuid";
const id: string = uuid();

to call the uuid function to return a uuid string.

We run

npm install uuid
npm install --save-dev @types/uuid

to install the uuid package and save the type definition for the package as a dev dependency.

Categories
JavaScript Answers

How to fix ReferenceError: webpack is not defined error with JavaScript?

To fix ReferenceError: webpack is not defined error with JavaScript, we require the webpack module.

For instance, we write

const webpack = require("webpack");

to require the webpack module with require.

Categories
JavaScript Answers

How to run multiple statements in one query with node-mysql?

To run multiple statements in one query with node-mysql, we call the query method.

For instance, we write

connection.query("SELECT ?; SELECT ?", [1, 2], (err, results) => {
  if (err) {
    throw err;
  }

  console.log(...results);
});

to call query with the select statements separated by a semicolon.

the array are the values that fill the question mark placeholders.

And we get the results from the results array.