Categories
JavaScript Answers

How to launch a browser from the a Node.js command line script?

Sometimes, we want to launch a browser from the a Node.js command line script.

In this article, we’ll look at how to launch a browser from the a Node.js command line script.

How to launch a browser from the a Node.js command line script?

To launch a browser from the a Node.js command line script, we can use the open package.

To install it, we run

npm install --save open

Then we use it by writing

const open = require("open");

(async () => {
  await open("unicorn.png", { wait: true });
  await open("https://example.com");
  await open("https://example.com", { app: "firefox" });
  await open("https://example.com", {
    app: ["google chrome", "--incognito"],
  });
})();

to call open with a file path with

await open("unicorn.png", { wait: true });

We set wait to true to wait for the file to open.

Also, we can call it with a URL with

await open("https://sindresorhus.com");

to open the browser with the URL.

We specify the app option to set the program to open.

Also, we can pass in command line arguments for the app being opened with

await open("https://sindresorhus.com", {
  app: ["google chrome", "--incognito"],
});

Conclusion

To launch a browser from the a Node.js command line script, we can use the open package.

Categories
JavaScript Answers

How to wait for all images to load then take screenshot with Puppeteer?

Sometimes, we want to wait for all images to load then take screenshot with Puppeteer.

In this article, we’ll look at how to wait for all images to load then take screenshot with Puppeteer.

How to wait for all images to load then take screenshot with Puppeteer?

To wait for all images to load then take screenshot with Puppeteer, we can use the waitUntil option with the page.goto method.

For instance, we write

await page.goto("https://www.example.com/", { waitUntil: "networkidle0" });

to go to https://www.example.com/ with goto and set the waitUntil option to 'networkidle0' to wait for everything to finish loading before resolving the returned promise.

Conclusion

To wait for all images to load then take screenshot with Puppeteer, we can use the waitUntil option with the page.goto method.

Categories
JavaScript Answers

How to toggle between multiple .env files like .env.development with Node.js?

Sometimes, we want to toggle between multiple .env files like .env.development with Node.js.

In this article, we’ll look at how to toggle between multiple .env files like .env.development with Node.js.

How to toggle between multiple .env files like .env.development with Node.js?

To toggle between multiple .env files like .env.development with Node.js, we can use the dotenv package.

To install it, we run

npm i dotenv

Then we use it by writing

require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` });

to require the dotenv module.

Then we call config with an object with the path set to the path of the config file we want to use.

Conclusion

To toggle between multiple .env files like .env.development with Node.js, we can use the dotenv package.

Categories
JavaScript Answers

How to write to a CSV in Node.js?

Sometimes, we want to write to a CSV in Node.js.

In this article, we’ll look at how to write to a CSV in Node.js.

How to write to a CSV in Node.js?

To write to a CSV in Node.js, we can use the fs.writeFile method,.

For instance, we write

const fs = require("fs");
const dataToWrite = "...";

fs.writeFile("./form-tracking/formList.csv", dataToWrite, "utf8", (err) => {
  if (err) {
    console.log("error");
  } else {
    console.log("saved!");
  }
});

to call fs.writeFile with the file path to write to.

dataToWrite has our CSV string that we want to write to the file path.

Then callback is called when the write operation is done or failed.

err in the callback has the error.

Conclusion

To write to a CSV in Node.js, we can use the fs.writeFile method,.

Categories
JavaScript Answers

How to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment?

Sometimes, we want to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment.

In this article, we’ll look at how to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment.

How to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment?

To fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment, we need to install our app’s dependency in the container.

To do this, we write

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

in our Dockerfile for creating our app’s container.

We copy package.json to our container with COPY package.json /usr/src/app/.

And we install the packages with RUN npm install.

Conclusion

To fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment, we need to install our app’s dependency in the container.