admin管理员组

文章数量:1431037

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');

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');

Share Improve this question edited Jun 5, 2018 at 20:50 Jowsey asked Jun 5, 2018 at 20:49 JowseyJowsey 571 silver badge8 bronze badges 6
  • 5 No, there is not. You cannot run PHP or on the client side either. – Bergi Commented Jun 5, 2018 at 20:50
  • node.js runs on the server, not the client. – Barmar 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 monjs require though – Bergi Commented Jun 5, 2018 at 20:51
  • Possible duplicate of JavaScript require() on client side – Wayrex 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? – user5734311 Commented Jun 5, 2018 at 21:00
 |  Show 1 more ment

1 Answer 1

Reset to default 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/

本文标签: javascriptIs there any way to run Nodejs in a ltscriptgt tagStack Overflow