question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Figure out how to pass a Node script to BROWSER

See original GitHub issue

We added support for BROWSER env variable that is interpreted by OS-specific command. On OS X, it runs open http://url.com -a <BROWSER>.

However I’m not sure how to make it execute a Node script, and if it’s even possible. That would be the best for allowing use cases like #1096 and https://github.com/facebookincubator/create-react-app/issues/1450. It would be awesome to investigate how to get it working with a Node script (my naïve attempt at pointing it to a shell script with node env didn’t seem to work), and maybe implement some custom handling for JS extension if we don’t find a solution.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sunknudsencommented, Jan 11, 2019

Found a way to achieve this using the script option on MacOS.

The following works if a development profile has been created.

https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager

Set BROWSER=open.js in .env.

// Content of open.js
const { spawn } = require('child_process');

spawn(
  '/Applications/Firefox.app/Contents/MacOS/firefox-bin',
  ['-p', 'development', process.argv[2]],
  { detached: true }
);
process.exit(0);

Then run npm start as usual.

1reaction
GAumalacommented, Feb 13, 2017

I think that the best choice is to spawn a child process to execute the node script if the BROWSER string ends in “.js”. It is the easiest way to support al operating systems.

react-dev-utils uses opn to open the browser. This module uses the command open on OS X, start on Windows and xdg-open on other platforms. I only use Linux, so I only know about xdg-open, but I believe the other tools are designed in a similar way.

xdg-open is meant to be used in desktop sessions, like when the user double clicks a file. You click a photo, your photo viewer is opened. You click a video, your media player is opened. In settings, the user can choose what programs to use for each type of file. For security reasons, it is strongly discouraged to execute scripts when double clicked, opening the file with a text editor is a much better choice. This is what probably every Linux desktop environment does by default, since users could be tricked into double clicking malicious scripts.

Handling the “.js” extension is much easier and secure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to interact with a node.js script in the browser?
Then, I want to pass the somevariable to the node script myfile.js, get the result back from myfile.js, and present the result in...
Read more >
How to run Node.js (apps) in the browser? - CloudBoost
For my port I actually started out by passing process = {} to the bootstrapping function, having no clue how things work or...
Read more >
How to share code between Node.js and the browser?
In this article, we will explore how to write JavaScript modules that can be used by both the client-side and the server-side applications....
Read more >
Run Node.js scripts from the command line
Run Node.js scripts from the command line · Pass string as argument to node instead of file path · Restart the application automatically....
Read more >
Node JS Tutorial: The Basics
Start a web browser (Chrome, Firefox). Issue URL http://localhost:3000/ . You shall see a hello-world message. To stop the server, press Ctrl-C ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found