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.

Categories
JavaScript Answers

How to fix the NPM “ENOENT: no such file or directory error” when installing Sails.js dependencies error with Node?

To fix the NPM "ENOENT: no such file or directory error" when installing Sails.js dependencies error with Node, we remove the package-lock.json file.

We remove the package-lock.json file and then re-run

npm install

to fix the error.

Categories
JavaScript Answers

How to include CSS files using Node, Express, and ejs?

To include CSS files using Node, Express, and ejs, we serve them as static files.

For instance, we write

app.use(express.static(__dirname + "/public"));

to serve the /public folder as a static files folder with express.static.

Then we add the css file in our ejs template with

<link rel="stylesheet" type="text/css" href="css/style.css" />