Some dependencies are not zipped in the bundle if they are declared via a local path
See original GitHub issue- Do you want to request a feature or report a bug? Bug
- What is the current behavior? The function is zipped and deployed correctly. However the function does not work properly because of a missing dependency.
- If the current behavior is a bug, please provide the steps to reproduce. Go to https://nx-crusher.netlify.com/.netlify/functions/main/graphql
You will see the following stack trace :
{
"errorMessage": "Cannot find module 'apollo-server-core'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:474:25)",
"Module.require (module.js:596:17)",
"require (internal/module.js:11:18)",
"Object.<anonymous> (/var/task/src/node_modules/@nestjs/graphql/dist/graphql-definitions.factory.js:13:30)",
"Module._compile (module.js:652:30)",
"Object.Module._extensions..js (module.js:663:10)",
"Module.load (module.js:565:32)",
"tryModuleLoad (module.js:505:12)",
"Function.Module._load (module.js:497:3)"
]
}
I tried to troubleshoot the issue:
It looks like my function depends on @nestjs/graphql package, which uses apollo-server-express : https://github.com/nestjs/graphql/blob/master/package.json#L27
apollo-server-express depends on apollo-server-core : https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-express/package.json#L35
But as you can see, apollo-server-core dependency is declared via a local path.
Not sure if it’s related to the current bug, but my intuition tells me it is.
- What is the expected behavior? Dependencies declared with local path should be bundled in the zip archive.
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (6 by maintainers)

Top Related StackOverflow Question
Thanks for the repro repository! I managed to figure out what’s going on.
Fix / workaround
You should:
npm install apollo-server-corerequire('apollo-server-core');statements in the function files that use@nestjspackagesExplanation
The key problem is that some Node modules do this:
Or this:
A common example is
node-fetchwhich conditionally requireencoding.encodingis only used with thetextConvertedmethod, which throws if it’s missing.Another example (in your case) is
@nestjs/graphqlwhich usesapollo-server-corebut does not declare it in its productiondependencies.The reason why some modules might want to do this and not use
optionalDependenciesis to allow users to opt-in to installing specific modules. In those cases,zip-it-and-ship-itcannot know at build-time whether you intend to use that opt-in dependency or not at runtime. In fact, we only look for nested dependencies (node modules used by other node modules) by checking the dependencies declared in the module’spackage.json, not by looking forrequire()statements inside the module’s code.Solution
When a module is conditionally required by another module, and that “conditional module” is used at runtime, the user should explicitly call that “conditional module” in their function file, i.e. make a noop
require()call to it. This ensures it gets bundled in the zip archive.This is a workaround. A proper solution would be for users to explicitly add specific modules to the zip archive.
Note that this is only when the “conditional” module is used from another module. When a conditional
require()call is made directly inside a function file (as opposed from another module), we always throw if the node module cannot be found. In the future, we should allow users to explicitly ignore specific modules when bundling.I’m happy that you making some progress on this.
For your migration file, you should be able to include in your archive by simply requiring it from your function file. This can be an noop
require()statements. That statement could be put inside anif (false) {}block if you don’t want to execute it. This is basically a hack forzip-it-and-ship-itto find that dependency and bundle it. As discussed in #68, we are considering adding a real solution to this problem.