Categories
JavaScript Answers

How to fix the ‘first argument must be a string or Buffer – when using response.write with http.request’ with Node.js?

To fix the ‘first argument must be a string or Buffer – when using response.write with http.request’ with Node.js, we call write with a string or buffer.

For instance, we write

res.write(response.statusCode.toString());

to call write with the statusCode value converted to a string with toString to avoid the error.

Categories
JavaScript Answers

How to fix Intellij Idea warning – “Promise returned is ignored” with async/await with JavaScript?

To fix Intellij Idea warning – "Promise returned is ignored" with async/await with JavaScript, we should make sure we run the promise.

For instance, we write

await userController.login(req, res);

to call the userController.login method that returns a promise.

And then we use await to run the promise and wait for it to finish.

Categories
JavaScript Answers

How to create an Excel File with Node.js?

To create an Excel File with Node.js, we use the xlsx module.

For instance, we write

const XLSX = require("xlsx");

const wb = XLSX.utils.book_new();
XLSX.writeFile(wb, "out.xlsx");

to call the book_new method to create a new Excel workbook.

Then we call writeGile to write the wb workbook to out.xlsx.

Categories
JavaScript Answers

How to install and run Mocha, the Node.js testing module?

To install and run Mocha, the Node.js testing module, we install the mocha package globally.

To install it, we run

npm install --global mocha

to install mocha globally.

We can also install it in the project folder as a dev dependency with

npm install --save-dev mocha

Then in package.json, we add

{
  //...
  "scripts": {
    "test": "mocha"
  }
  //...
}

to add the test script to run mocha installed in the node_modules folder.

Categories
JavaScript Answers

How to fix ‘ng: command not found’ error with Angular?

To fix ‘ng: command not found’ error with Angular, we install the @angular/cli package.

To install it, we run

npm i -g @angular/cli