Categories
JavaScript Answers

How to fix missing write access in mac to the /usr/local/lib/node_modules folder on Mac?

To fix missing write access in mac to the /usr/local/lib/node_modules folder on Mac, we’ve to set the folder’s owner to the current user.

For instance, we run

sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
sudo chown -R $USER /usr/local/lib/node_modules

to run chown to set the owner of the ~/.npm, /usr/lib/node_modules and the /usr/local/lib/node_modules folders to the current user.

Categories
JavaScript Answers

How to find user by username LIKE value with Mongoose.js and JavaScript?

To find user by username LIKE value with Mongoose.js and JavaScript, we use the findOne method with a regex.

For instance, we write

const name = "Peter";
model.findOne({ name: new RegExp("^" + name + "$", "i") }, (err, doc) => {
  //...
});

to call findOne to find the document with field name like the 'Peter' by setting name to a regex with name in it.

We use the i flag to search in a case insensitive manner.

We get result from doc in the callback.

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.