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.

Categories
JavaScript Answers

How to upload file using POST request in Node.js?

To upload file using POST request in Node.js, we call the request function.

For instance, w write

request(
  {
    url: "http://example.com",
    method: "POST",
    formData: {
      regularField: "someValue",
      regularFile: someFileStream,
      customBufferFile: {
        value: fileBufferData,
        options: {
          filename: "myfile.bin",
        },
      },
    },
  },
  handleResponse
);

to call request to make a post request to http://example.com

We set formData to an object with the key-value pairs for the form data object.

The file is set as the value of regularFile.

We set it to a stream.

handleResponse is a function that’s called when a response is received.

Categories
JavaScript Answers

How to install LTS version of Node.js via homebrew?

To install LTS version of Node.js via homebrew, we run the brew install command.

To install it, we run

brew install node@8

to install Node 8.

Then we link it with

brew link --force node@8

to make it visible.