serverless-heaven / serverless-webpack

Serverless plugin to bundle your lambdas with Webpack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot include dependencies not required by bundle (knex pg).

jonni-larjomaa opened this issue · comments

commented

Hi,

I'm using knex and bookshelf with pg-library, the problem is that the pg library does not get bundled in to node_modules directory because it is not required in the files that get bundled (but is required by knex on runtime). This causes AWS Lambda to throw following errors.

2017-09-08T07:28:40.829Z 52dbac4f-9467-11e7-af3a-fdbf9a737897 Knex: run
$ npm install pg --save
Error: Cannot find module 'pg'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Client_PG._driver (/var/task/node_modules/knex/lib/dialects/postgres/index.js:91:12)
at Client_PG.initializeDriver (/var/task/node_modules/knex/lib/client.js:219:26)
at Client_PG.Client (/var/task/node_modules/knex/lib/client.js:113:10)
at new Client_PG (/var/task/node_modules/knex/lib/dialects/postgres/index.js:56:20)
at Knex (/var/task/node_modules/knex/lib/index.js:60:34)
at Object.module.exports.dev.elasticsearch (/var/task/index.js:102:35)
at webpack_require (/var/task/index.js:20:30)
at Object. (/var/task/index.js:79:17)
at webpack_require (/var/task/index.js:20:30)
at Object. (/var/task/index.js:270:12)
at webpack_require (/var/task/index.js:20:30)
at Object. (/var/task/index.js:217:15)

Probably the cause is that this is not direct depency but resolved one during runtime which is then not resolved by webpack during bundling. I would appreciate a lot if this could work, although i could also probably just:

var dump = require("pg");

in order to get it bundled for now

commented

And yes the pg-library can be found from package.json and also from node_modules folder

Hi @jonni-larjomaa . Thanks for bringing this up 👍 .

Yes, currently it is indeed so, that Webpack optimizes the package and reports the needed dependencies to the plugin, that in turn uses them to build a working package. As long as webpack does not know that a specific module is needed, although it is not referenced, it will not be packaged.

Good to know that it works with your workaround.

This is a missing feature that could be implemented quite easily. My proposal would be that we just use the custom.webpackIncludeModules flag which currently only can be true to control whether modules are packaged or not.
The flag can be extended to be optionally an array of modules that are forced to be packaged, even if they are not referenced after the webpack compile anymore.

In your case you would then specify:

custom:
  webpackIncludeModules:
    - pg

This will then do a forced package of the pg module.

Released with 3.1.0

The current syntax for forced includes is a bit different than in @HyperBrain's last comment above.