1

Is it possible to run a Node.js file from the serverside from JavaScript? If so how would i? I have googled some but cant find a real answer to this. Thanks

4
  • 1
    Can you add more details about the file you want to run? What are you trying to accomplish? Typically code written for the client side is built using something like webpack. Commented Sep 12, 2018 at 7:13
  • Only if there's a process that's listening on the server for the specific requests and that has the capability to run said file. Commented Sep 12, 2018 at 7:16
  • @Chris I want to run a node.js file that contains a csv writer, just a promise that runs once Commented Sep 12, 2018 at 7:39
  • And that csv writer returns a string? As mentioned in the answers, it'll work so long as you don't use certain node features unavailable in the browser. You should give more detail about your running environment/app so people can advise you on how to work the module in. Commented Sep 12, 2018 at 7:42

3 Answers 3

1

NodeJS is JavaScript on the server-side environment.

What you refer as "JavaScript" is actually the same JavaScript, just in your browser environment.

What you can do: you can use Webpack to create a NodeJS app, that will automatically create an index.html and put it on a server. With webpack, you can also import NodeJS modules in your frontend code.

For example:

main.js:

import './index.html';
import $ from 'jquery';

$(() => {
    console.log('jquery is ready');
    $('#root').append('<h2>How are you doing?</h2>');
});

index.html:

<html>
    <head>
    </head>
    <body>
        <div id="root"></div>
        <script src="bundle.js"></script>
    </body>
</html>

The bundle.js will be generated by Webpack.

Sign up to request clarification or add additional context in comments.

Comments

0

If this file uses nodejs API like Streams, HTTP, File System etc, it is impossible, cause browser is not able to run these API.

Comments

0

there are some basic differences between browser js env and node server env. Example: WEB API is availble for browser js env for node its different.

Moreover node ecosystem has alot more support for several other functinalities like file system handling etc.

Since node is developed for server side programming,you should pay proper respect to the purpose and not try to run it in browser :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.