14

Consider I have been done with my NodeJs with Express and MongoDB Application. I can bundle up my application to deliver the same to client but in that case client have to install all required npm modules and change mongoDB connection url. So to counter this issue:

Is there any other way to bundle my application so client do not need to install any npm modules to run application? If yes, then how client can connect to his mongodb?

user2672420
  • 303
  • 2
  • 4
  • 14
  • Why is it a problem for them to install the NPM modules? – skwidbreth Nov 22 '17 at 04:32
  • @skwidbreth: I do not want client to open his command prompt and run npm install. – user2672420 Nov 22 '17 at 04:39
  • Have you seen https://stackoverflow.com/questions/14314038/how-do-i-deploy-node-js-applications-as-a-single-executable-file – Jomy Joseph Nov 22 '17 at 04:49
  • 3
    If all your modules are installed locally (not globally), you can zip the entire hierarchy, including all NPM modules in the node_modules directory and then they can just unzip that whole hierarchy. As for the location of the MongoDB application, you should code your app so that can be controlled via either an environment variable or a config file that the customer can edit to specify where their database is. – jfriend00 Nov 22 '17 at 05:28
  • @user2672420 Then I'd second jfriend00's suggestion above ^^ – skwidbreth Nov 22 '17 at 14:14

2 Answers2

16

pkg by Vercel works really well for me. It bundles everything into a single executable that runs out of a terminal. This makes it really easy to distribute.

To connect to a Mongo instance, you would have to have some kind of configuration file that the user edits. I recommend putting into the user data folder (Application Support on Mac and AppData on Windows), but you could have it sit right next to the packaged executable.

Alex Anderson
  • 660
  • 6
  • 16
  • Is it possible to include the mongodb installation into the executable created by pkg? The use case being that a local mongo instance will be used by the node app but is not actually installed on the system. – MrfksIV Nov 21 '18 at 14:18
  • Gosh, I don't know if that's possible. Mongo is its own executable and runs in it's own runtime separate from node. I'm sure you could figure some way of bundling the mongod executable with the pkg app and running it from within Node using the command line, but that seems less than ideal. The better option would be bundling a Node-based database, like LevelDB or LokiJS. – Alex Anderson Nov 22 '18 at 16:05
1

You can use good old webpack by setting it's target to node. here is a simple webpack.config.js:

import webpack from 'webpack';

export default {
  entry: "./server.js",
  output: {
    // output bundle will be in `dist/buit.js`
    filename: `built.js`,
  },
  target: 'node',
  mode: 'production',
  // optional: bundle everything into 1 file
  plugins: [
    new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1
    })
  ],
};
Mechanic
  • 5,015
  • 4
  • 15
  • 38