Categories
JavaScript Answers

How to fix Module not found: Can’t resolve ‘fs’ in Next.js application with JavaScript?

To fix Module not found: Can’t resolve ‘fs’ in Next.js application with JavaScript, we set the fs option to false.

For instance, we write

module.exports = {
  webpack5: true,
  webpack: (config) => {
    config.resolve.fallback = { fs: false };

    return config;
  },
};

in webpack.config.js to set fs to false to disable the use of the fs module in the browser environment.

Categories
JavaScript Answers

How to pass variable to html template in Nodemailer?

To pass variable to html template in Nodemailer, we put them in the context property.

For instance, we write

const mailOptions = {
  from: "abc@gmail.com",
  to: "username@gmail.com",
  subject: "Sending email",
  template: "yourTemplate",
  context: {
    username,
    whatever: variable,
  },
};

const info = await transporter.sendMail(mailOptions);

to call sendMail with mailOptions to send the email message.

We set the context property to an object with the template variables to interpolate them in the template.

Categories
JavaScript Answers

How to remove a key with Node redis?

To remove a key with Node redis, we call the del method.

For instance, we write

redis.del('SampleKey');

to remove the entry with key 'SampleKey‘ from Redis with del.

Categories
JavaScript Answers

How to find a Node.js app running and kill it?

To find a Node.js app running and kill it, we run the ps and kill commands.

To find the node process number, we run

$ ps -e | grep node

with ps.

We find the node process with grep.

And then we run

$ kill -9 132

to run kill to kill the process with ID 132 where 132 is returned by ps -e | grep node.

We replace this with the actual ID of the process.

Categories
JavaScript Answers

How to wait for a stream to finish piping with Node?

To wait for a stream to finish piping with Node, we use the stream/promises module.

For instance, we write

import * as StreamPromises from "stream/promises";

//...
await StreamPromises.pipeline(sourceStream, destinationStream);

to call StreamPromises.pipeline to pipe sourceStream to destinationStream.

We use await to wait for the promise returned to finish before proceeding.