Orchid
Orchid

Reputation: 73

How does zeit/pkg bundle my node application?

This is my first question on SO so first of all a BIG HELLO and please excuse in case my question doesn't fit all critereas or seems akward.

I would like to know how my app is bundled and compiled with zeit's pkg.

Is the bundle inserted into the pre-sized binary?

Does it download the node sources like nexe and is code run as main or ist the snapshot loaded when the binary is executed?

I have a backend node-express server and a frontend angular app with each having their own node_modules folder like so:

myapp
 |-------/client
 |          |
 |          |-----/e2e
 |          |-----/node_modules
 |          |-----/src
 |           
 |-------/config 
 |
 |-------/node_modules
 |
 |-------/routes
 |
 |--app.js
 |
 |--package.json
 |
 |--README.md

How does pkg treat those two instnces of node_modules?

Your help/expertise is highly appreciated!

Upvotes: 5

Views: 3785

Answers (1)

Estus Flask
Estus Flask

Reputation: 222503

The fact that there are several node_modules doesn't matter. In app.js, require('foo') refers to myapp/node_modules/foo. Modules are bundled in a similar way they are resolved when they are loaded.

pkg packages only modules that are specified explicitly. As the documentation states,

During packaging process pkg parses your sources, detects calls to require, traverses the dependencies of your project and includes them into executable. In most cases you don't need to specify anything manually. However your code may have require(variable) calls (so called non-literal argument to require) or use non-javascript files (for example views, css, images etc).

  require('./build/' + cmd + '.js')
  path.join(__dirname, 'views/' + viewName)

Such cases are not handled by pkg. So you must specify the files - scripts and assets - manually in pkg property of your package.json file.

  "pkg": {
    "scripts": "build/**/*.js",
    "assets": "views/**/*"
  }

Just be sure to call pkg package.json or pkg . to make use of scripts and assets entries.

Not doing that will result in a warning during packaging:

Dynamic require may fail at run time, because the requested file is unknown at compilation time and not included into executable. Use a string literal as an argument for 'require', or leave it as is and specify the resolved file name in 'scripts' option.

Upvotes: 3

Related Questions