3

I would like to run Node.js in my webpage, the same way that you would with PHP or ASP.NET.

The reason for this is I would like to use require()'d modules from NPM in my browser javascript, however vanilla JS doesn't seem to support this. Is there any way I can run Node.js in my tags? If not, would requiring examplenodefile.js in my browser javascript work?

Edit: Is there any alternative to this, if it isn't possible? I know I can set script src but that doesn't really work as nicely as var foo = require('bar');

6
  • 5
    No, there is not. You cannot run PHP or .net on the client side either. Commented Jun 5, 2018 at 20:50
  • node.js runs on the server, not the client. Commented Jun 5, 2018 at 20:50
  • Of course, many of the modules from npm work in the browser as well, you might have to use a module bundler that supports commonjs require though Commented Jun 5, 2018 at 20:51
  • Possible duplicate of JavaScript require() on client side Commented Jun 5, 2018 at 20:51
  • You need to tell us what your end goal is. Building websites with node (express) requires you to use templates and the like, and switching to that from PHP can be challenging. Plus, you obviously won't be able to run arbitrary node code in a browser environment anyway, given the restrictions on, say, file access and the like. Is this about a specific module? Or just about having to trouble to wrap your head around templating? Commented Jun 5, 2018 at 21:00

1 Answer 1

4

From your above question I am assuming you want to use NPM modules in your browser side JavaScript. This is possible to do assuming you have web pack. You can use webpack to bundle the NPM packages inside a bundle file.

Check out webpack...

https://webpack.js.org/

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

13 Comments

This looks to be exactly what I was looking for. Just a simple way to use NPM modules in my websites. I'll accept when it lets me, thanks!
You're welcome. It can take some time to set up but when you set it up its a very powerful tool. Happy reading!
Again, many npm modules will not work in a client environment at all, webpack or not.
I use quite a bit of npm packages in my bundles. I haven't ran into to many that don't work unless they were built strictly for server side use, like an http server or a server side templating engine.
@MicahBell Sure, but I suspect OP does want to do server-side stuff right in his <script> tags, given that he referenced PHP and ASP.NET.
|

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.