Categories
JavaScript Answers

How to use Node.js to open default browser and navigate to a specific URL?

Spread the love

Sometimes, we want to use Node.js to open default browser and navigate to a specific URL.

In this article, we’ll look at how to use Node.js to open default browser and navigate to a specific URL.

How to use Node.js to open default browser and navigate to a specific URL?

To use Node.js to open default browser and navigate to a specific URL, we can use the open package.

To install it, we run

npm install open

Then we use it by writing

const open = require("open");

open("http://example.com");
open("http://example.com", { app: "firefox" });

to call open to open the URL we pass into it.

We can also call it with an object that has some options.

We set app to 'firefox' to open the URL in Firefox.

Conclusion

To use Node.js to open default browser and navigate to a specific URL, we can use the open package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *