mongodb-js / boxednode

📦 boxednode – Ship a JS file with Node.js in a box
Apache License 2.0
599 stars 10 forks source link

Is it possible to include node_modules in the final binary? #14

Closed saurabh-deep closed 3 years ago

saurabh-deep commented 3 years ago

Hey folks!

First of all, congratulations and thank you for coming up with this project. I couldn't find any other option that will create a sign-able Mac OS binary, once node-packer was abandoned.

I had to also include some external node module that I am using in my script, say lodash for example. I thought I figured out how to use it despite the single file restriction. But it turned out the binary was working as long as the node_modules folder was also present in the same physical location as that of the binary. As soon as I moved the binary to a different path and the node_modules folder was not visible, it started throwing Cannot find module 'lodash' error.

I am wondering if there is a way to include node_modules also in the final binary. Appreciate your help / guidance on this.

Best Saurabh

addaleax commented 3 years ago

There’s a ton of different bundlers for JS files – we use parcel, but rollup, browserify, webpack, etc. should all be able to do this for you.

saurabh-deep commented 3 years ago

Thank you @addaleax for the super quick response. I will try these bundlers.

saurabh-deep commented 3 years ago

Hello @addaleax! Unfortunately, some of the dependencies that we have in our project are not playing well with the bundlers, because of dynamic dependencies and need to read files from the dependency folders at the run time. Some don't work well with Webpack and some don't work well with Parcel. This effectively stops us from being able to use bundlers to come up with a single JS file that works well. I am yet to try Rollup though.

We all know pkg and nexe do not create sign-able binaries and node-packer (which did create almost perfect binaries till Node v10) has been abandoned. I have no idea where to go from here.

I cannot stop myself from asking - Do you have this on the roadmap to include node_modules also along with the entry file? This is something node-packer did very nicely. It included everything in your project, packed that in a virtual file system and compile all that with the Node.js source code. It was an awesome project, but no one is there to maintain the project anymore, since it needs extensive C/C++ knowledge. 😞

addaleax commented 3 years ago

Do you have this on the roadmap to include node_modules also along with the entry file?

No, that’s way out of scope for this project, sorry.

saurabh-deep commented 3 years ago

Do you have this on the roadmap to include node_modules also along with the entry file?

No, that’s way out of scope for this project, sorry.

Understood. Thanks for clarifying the same.

How do you generally deal with the situation when you encounter a dependency that doesn't play well with the bundler (Parcel in your case)?

Some times the dependency is direct and that is easier to deal with by using a different dependency. But when the dependency which is playing spoilsport (shelljs as an example) is an indirectly dependency (used by another package on which we have no control), that's a very tricky one.

addaleax commented 3 years ago

How do you generally deal with the situation when you encounter a dependency that doesn't play well with the bundler (Parcel in your case)?

We’ve had a few cases where we needed to monkey-patch parcel directly to address this, and submitted PRs to dependencies like https://github.com/reklatsmasters/saslprep/pull/9. But that doesn’t happen here in boxednode, we’re really not trying to build another JS bundler here or to build a virtual filesystem implementation.

saurabh-deep commented 3 years ago

Got it. Thank you @addaleax!