0

Correct me if I'm wrong, but AFAIK webpack implements a module system on the browser, which historically never had a module system.

But Node.js has a module system. You can use commonjs require or use ES6 imports and it will work fine. I've seen lots of posts over the internet on how to implement webpack bundling to a Node.js backend code.

Is this even recommended? Is it a good practice at all?

I think that there is a clear answer to this question out there. And it's not opinion-based.

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
  • 1
    This question is definitely opinion-based, as it relies on the specific situation you're in and you haven't told us at all what that situation is. Some folks bundle their Node.js apps to make it easier to distribute/deploy. There may be other use cases too. What is your situation? – Brad Feb 19 '21 at 22:30
  • why do y ou think you need a server-side bundle? – Joe Feb 19 '21 at 22:42
  • I've personally not found any reasons to want to bundle back-end code and it just complicates debugging and troubleshooting since the bundler has repackaged everything. There are a some people who find some advantages in doing so. See [Webpack for back-end?](https://stackoverflow.com/questions/37788142/webpack-for-back-end) for more info. – jfriend00 Feb 19 '21 at 22:45
  • Google is your friend here: [Do you need Webpack for server-side Javascript](https://www.getrevue.co/profile/masteringjs/issues/do-you-need-webpack-for-server-side-javascript-230766) and numerous other links discussing the topic. – jfriend00 Feb 19 '21 at 22:50
  • @jfriend00 thanks. From the article that you mentioned, I didn't find any concrete reason where one should use `webpack` to bundle server code. Even on `AWS Lambdas`, it mentions that you can't install `npm` dependencies (maybe on older versions), but today this is already possible, as per [AWS Lambda docs](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html). I guess it's a matter of taste, but so far I'll stick with my conclusion that `webpack` is meant for browser code, because it's an environment that lacks a module system, whilst Node.js has a module system out of the box. – cbdeveloper Feb 23 '21 at 14:56
  • 1
    @cbdeveloper - That is my conclusion too. – jfriend00 Feb 23 '21 at 15:00

0 Answers0