Categories
JavaScript Answers

How to convert string to number with Node.js?

To convert string to number with Node.js, we call the parseInt function.

For instance, we write

const year = parseInt(req.params.year, 10);

to call parseInt to convert req.params.year to a decimal integer.

Categories
JavaScript Answers

How to update npm on Windows?

To update npm on Windows, we run npm install.

We run

npm install npm@latest

to update npm to the latest version.

Categories
JavaScript Answers

How to delete a directory that is not empty with Node?

To delete a directory that is not empty with Node, we install rimraf.

To install it, we run

npm install rimraf

Then we write

rmdir = require("rimraf");
rmdir("some/directory/with/files", (error) => {});

to call rmdir to remove the some/directory/with/files folder.

If there’s any error, then we get the error from the callback.

Categories
JavaScript Answers

How to install and run MongoDB on Mac OS?

To install and run MongoDB on Mac OS, we install MongoDB with brew install.

We run

brew install mongodb

to install mongodb.

Then we make folder to hold the Mongo data files with

mkdir -p /data/db

Then we set permissions with

sudo chown -R `id -un` /data/db

We then run the server with

mongod

We run the Mongo command line utility with mongo.

Categories
JavaScript Answers

How to fix npm install gives error “can’t find a package.json file” with JavaScript?

To fix npm install gives error "can’t find a package.json file" with JavaScript, we run npm install with the package name.

For instance, we run

npm install express

to install the express package.

Then a package.json file is created automatically with a list of installed packages.