I'm writing an application using Node.js.
One of the functions I want to create is to open the default web browser and navigate to a specific URL.
I want it to be portable so that it runs on Windows/Mac/Linux.
I'm writing an application using Node.js.
One of the functions I want to create is to open the default web browser and navigate to a specific URL.
I want it to be portable so that it runs on Windows/Mac/Linux.
Use open (formerly known as opn
) because it will handle the cross platform issue. To install:
$ npm install open
To use:
const open = require('open');
// opens the url in the default browser
open('http://sindresorhus.com');
// specify the app to open in
open('http://sindresorhus.com', {app: 'firefox'});
open
seems to support Linux now. –
Bergama var url = 'http://localhost';
var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
require('child_process').exec(start + ' ' + url);
&
's in the URL should be escaped with ^&
–
Chamade node-open is deprecated. Now use open:
const open = require('open')
await open('http://sindresorhus.com') // Opens the url in the default browser
await open('http://sindresorhus.com', {app: 'firefox'}) // Specify the app to open in
Windows + Express
app.listen(3000, ()=>{
require('child_process').exec('start http://localhost:3000/');
});
Install:
$ npm install open
Usage:
const open = require('open');
(async () => {
// Opens the image in the default image viewer and waits for the opened app to quit.
await open('unicorn.png', {wait: true});
console.log('The image viewer app quit');
// Opens the URL in the default browser.
await open('https://sindresorhus.com');
// Opens the URL in a specified browser.
await open('https://sindresorhus.com', {app: 'firefox'});
// Specify app arguments.
await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
})();
You may need to implement a switch using the value of ...
require('os').type()
And then use spawn("open")
or spawn("xdg-open")
depending on the platform?
require('child_process').spawn('explorer', ['url'])
is a nice oneliner! –
Drake The easiest and neatest way, IMHO is using an npm package called openurl. Do a npm install openurl
. You could try this real quick in your Nodejs REPL
require("openurl").open("https://mcmap.net/q/126228/-how-to-use-nodejs-to-open-default-browser-and-navigate-to-a-specific-url")
You could also send emails with it if the need arises like so;
require("openurl").open("mailto:[email protected]")
Simply Use
require('child_process').exec('start https://www.google.co.in/');
It's Worked For me.
#!/usr/bin/env node
const url = 'http://localhost'
require('child_process')
.exec((process.platform
.replace('darwin','')
.replace(/win32|linux/,'xdg-') + 'open ' + url));
var url = '\\index.html';
var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
require('child_process').exec(start + ' ' + __dirname + url);
Lexa-B’s answer worked best for me, but I was getting “Windows could not locate index.html” error. I was using lexa-b code child_process exec command to open a local webpage within an npm package I was writing. Needed to open an html file in my npm package when running it / opening it from package bin.js with npx command.
All that was needed was to append __dirname to file path to ensure the relative directory path to the file calling the child_process was correct. The child_process was running at the home folder, which is far away from the npx temp file location. __dirname solves that problem and links the two, solving my missing file error.
© 2022 - 2024 — McMap. All rights reserved.