Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a JS file and now I want to run on chrome browser, just like we run html ; and I like to inform that I have node installed , please suggest solution ??

What I have tried:

I made the JS file and saved on Desktop
Then I open CMD and navigated to desktop
Then typed 'live-server',
Browser window opened showing 'file.js' with '~' (go to parent)
I clicked file.js but only code of file displayed
I was expecting output to be displayed like it displays in CMD when I type 'node file.js'
Posted
Updated 6-Aug-19 11:08am

1 solution

Quote:
I have node installed
How do you intend to do that? Like this,
JavaScript
alert("Node.js installed");
If so, that is a bad design and experience for the user, I can say this much.

What I would recommend is going back to creating an HTML file, and showing that file in Chrome (or any locally installed browser) and stating that Node.js has been installed. One thing to notice here is that you can use HTML, because you do not need to execute any code to check whether Node.js is installed or not, since you are executing this code from Node.js.
Quote:
I clicked file.js but only code of file displayed
There are securities in browsers, that would prevent the JavaScript code to be executed if you open a file directly. That is why you load this file directly in a script element so the browser can execute the code.

Thus, a good approach is to utilize the standard HTML file and show the content. One more thing, you can always insert the JavaScript in this HTML file using the script element.
Quote:
I was expecting output to be displayed like it displays in CMD when I type 'node file.js'
When you execute the node binary, that is not your browser or any JavaScript engine (I know, V8 is a JavaScript engine too) executing anything instead that is the Node.js executable running your code and logging the outputs of your application. If it is the logs that you want to see, you can maintain a connection between your Node.js process and that browser window and show the logs there.

You can try to pipe the output to other windows, such as browsers and tabs in the browser and have all the logs forwarded there, or maybe write it somewhere from where your browser or JavaScript will read and show in the tab. Maybe?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900