Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a multi-file node.js app to a single file?

Tags:

If I have a node.js application that is filled with many require statements, how can I compile this into a single .js file? I'd have to manually resolve the require statements and ensure that the classes are loaded in the correct order. Is there some tool that does this?

Let me clarify.

The code that is being run on node.js is not node specific. The only thing I'm doing that doesn't have a direct browser equivalent is using require, which is why I'm asking. It is not using any of the node libraries.

like image 957
ryeguy Avatar asked Apr 13 '11 21:04

ryeguy


People also ask

How do I copy files from one node js file to another?

copyFile() method is used to asynchronously copy a file from the source path to destination path. By default, Node. js will overwrite the file if it already exists at the given destination. The optional mode parameter can be used to modify the behavior of the copy operation.


1 Answers

You can use webpack with target: 'node', it will inline all required modules and export everything as a single, standalone, one file, nodejs module

https://webpack.js.org/configuration/target/#root

2021 edit: There are now other solutions you could investigate, examples.

Namely:

  • https://esbuild.github.io
  • https://github.com/huozhi/bunchee
like image 103
vvo Avatar answered Dec 01 '22 22:12

vvo