question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
ehmickycommented, Nov 5, 2019

Thanks for the repro repository! I managed to figure out what’s going on.

Fix / workaround

You should:

  • npm install apollo-server-core
  • add require('apollo-server-core'); statements in the function files that use @nestjs packages

Explanation

The key problem is that some Node modules do this:

try {
  require('moduleName')
} catch (error) {}

Or this:

if (condition) {
  require('moduleName')
}

A common example is node-fetch which conditionally require encoding. encoding is only used with the textConverted method, which throws if it’s missing.

Another example (in your case) is @nestjs/graphql which uses apollo-server-core but does not declare it in its production dependencies.

The reason why some modules might want to do this and not use optionalDependencies is to allow users to opt-in to installing specific modules. In those cases, zip-it-and-ship-it cannot 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’s package.json, not by looking for require() 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.

2reactions
ehmickycommented, Nov 7, 2019

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 an if (false) {} block if you don’t want to execute it. This is basically a hack for zip-it-and-ship-it to find that dependency and bundle it. As discussed in #68, we are considering adding a real solution to this problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local dependency in package.json - Stack Overflow
When I try the above and npm install from git+ssh it appears to look in the node_modules directory and not attempt to go...
Read more >
Distributing binary frameworks as Swift packages
To distribute code in binary form as a Swift package, create an XCFramework bundle, or artifact, that contains the binaries. Then, make the...
Read more >
Learning the Basics - Gradle User Manual
Declaring multiple repositories is helpful if some dependencies are only available in one repository but not the other. You can mix any type...
Read more >
Create a package using the nuget.exe CLI - Microsoft Learn
That command fails if the .nuspec contains any placeholders. From a convention-based working directory. Because a NuGet package is just a ZIP ......
Read more >
Deploy Ruby Lambda functions with .zip file archives
Install libraries in the vendor directory using the bundle command. bundle config set --local path 'vendor/bundle' \ bundle install. You should see ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found