Categories
JavaScript Answers

How to execute an external program from within Node.js?

Sometimes, we want to execute an external program from within Node.js.

In this article, we’ll look at how to execute an external program from within Node.js.

How to execute an external program from within Node.js?

To execute an external program from within Node.js, we can use the child_process module’s exec method.

For instance, we write

const {
  exec
} = require('child_process');

exec(command, (error, stdout, stderr) => {
  console.log(error, stdout, stderr)
});

to call exec with the command string we want to run.

The 2nd argument is a callback that has the stdout and stderr output after running the command.

error has the error that’s thrown when the command is run.

Conclusion

To execute an external program from within Node.js, we can use the child_process module’s exec method.

Categories
JavaScript Answers

How to delete a key from a MongoDB document using Mongoose?

Sometimes, we want to delete a key from a MongoDB document using Mongoose.

In this article, we’ll look at how to delete a key from a MongoDB document using Mongoose.

How to delete a key from a MongoDB document using Mongoose?

To delete a key from a MongoDB document using Mongoose, we can use the schema update method.

For instance, we write

User.update({
  _id: user._id
}, {
  $unset: {
    field: 1
  }
}, callback);

to call update on the entry with the given _id value.

Then we use the $unset operator to remove the field property from the User entry.

The callback is a function that runs when the update operation is done.

Conclusion

To delete a key from a MongoDB document using Mongoose, we can use the schema update method.

Categories
JavaScript Answers

How to print a list of all installed Node.js modules?

Sometimes, we want to print a list of all installed Node.js modules.

In this article, we’ll look at how to print a list of all installed Node.js modules.

How to print a list of all installed Node.js modules?

To print a list of all installed Node.js modules, we can use npm ls.

For instance, we run

npm -g ls --depth=0

to print all the installed global packages.

And we run

npm ls --depth=0

to print all the installed local packages in a folder.

Conclusion

To print a list of all installed Node.js modules, we can use npm ls.

Categories
JavaScript Answers

How to use Nodemailer with Gmail and Node.js?

Sometimes, we want to use Nodemailer with Gmail and Node.js.

In this article, we’ll look at how to use Nodemailer with Gmail and Node.js.

How to use Nodemailer with Gmail and Node.js?

To use Nodemailer with Gmail and Node.js, we use the createTransport and sendMail methods.

For instance, we write

const mailer = require("nodemailer");
const smtpTransport = mailer.createTransport("SMTP", {
  service: "Gmail",
  auth: {
    user: "abc@gmail.com",
    pass: "password"
  }
});

const mail = {
  from: "from <from@gmail.com>",
  to: "to@gmail.com",
  subject: "Send Email Using Node.js",
  text: "Node.js",
  html: "<b>Node.js</b>"
}

smtpTransport.sendMail(mail, (error, response) => {
  if (error) {
    console.log(error);
  } else {
    console.log(response.message);
  }
  smtpTransport.close();
});

to call mailer.createTransport to create the smtpTransport object that we use to log into our SMTP server.

Then we call smtpTransform.sendMail to send our email with the data coming from the mail object.

If there’s an error, then error is set in the callback.

Otherwise, response is set.

Finally, we log out of the SMTP server with smtpTransport.close.

Conclusion

To send emails in Node.js, we can use the nodemailer package.

Categories
JavaScript Answers

How to zip an entire directory using Node.js?

Sometimes, we want to zip an entire directory using Node.js.

In this article, we’ll look at how to zip an entire directory using Node.js.

How to zip an entire directory using Node.js?

To zip an entire directory using Node.js, we can use the archiver package.

We install the package by running

npm i archiver

Then we write

const fs = require('fs');
const archiver = require('archiver');

const output = fs.createWriteStream('target.zip');
const archive = archiver('zip');

output.on('close', () => {
  console.log(archive.pointer() + ' total bytes');
});

archive.on('error', (err) => {
  throw err;
});

archive.pipe(output);
archive.directory(sourceDir, false);
archive.directory('subdir/', 'new-subdir');

archive.finalize();

to call fs.createWriteStream with the path to the zip file.

And then we call archiver with 'zip' to create a zip file.

We call archive.pipe with output to pipe the content directory content to the zip file.

Then we call archive.directory with the directories we want to put in the zip file.

We call output.on with 'close' to run the callback when the write stream is done.

And we call archive.on with 'error' to listen for errors when making the zip file.

Finally, we call archive.finalize to finish the archiving operation.

Conclusion

To zip an entire directory using Node.js, we can use the archiver package.