FormidableLabs / serverless-jetpack

A faster JavaScript packager for Serverless applications.
MIT License
277 stars 21 forks source link

Resolution error: cannot find module #184

Open samrith-s opened 3 years ago

samrith-s commented 3 years ago

Hello,

I am trying to bundle my NestJS app using Jetpack. I get this error:

Serverless: [serverless-jetpack] Packaging 2 functions, 0 services, and 0 layers with concurrency 4

  Error --------------------------------------------------

  Error: Encountered resolution error in /Users/sam/Work/personal/test-api/dist/apps/hello/lambda.js for @nestjs/microservices/microservices-module: Error: Cannot find module '@nestjs/microservices/microservices-module' from '/Users/sam/Work/personal/test-api/dist/apps/hello'
      at /Users/sam/Work/personal/test-api/node_modules/trace-deps/lib/trace.js:278:17
      at async Promise.all (index 0)
      at async traceFile (/Users/sam/Work/personal/test-api/node_modules/trace-deps/lib/trace.js:231:19)
      at async _recurseDeps (/Users/sam/Work/personal/test-api/node_modules/trace-deps/lib/trace.js:101:22)
      at async traceFiles (/Users/sam/Work/personal/test-api/node_modules/trace-deps/lib/trace.js:349:19)
      at async Object.globAndZip (/Users/sam/Work/personal/test-api/node_modules/serverless-jetpack/util/bundle.js:512:20)

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.6.0
     Framework Version:         2.17.0 (local)
     Plugin Version:            4.4.1
     SDK Version:               2.3.2
     Components Version:        3.4.3

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I need Jetpack to ignore this as the module is a dynamic dependency of NestJS and is not actually consumed by my application. How do I make this work?

ryan-roemer commented 3 years ago

What does the relevant import look like in apps/hello/lambda.js that is bringing this dependency in?

ryan-roemer commented 3 years ago

I was guessing it would be https://github.com/nestjs/nest/blob/8c503d3193059f40632df7378d5091ea8ef6b13d/packages/core/nest-application.ts#L34-L38 which would mean a configuration to allow misses like:

jetpack:
  trace:
    allowMissing:
      "@nest/core":
        - "@nest/microservices"
        - # ... etc ...

... which is usually how this ends.

But it looks like your application entry point has the optional require, which I'm guessing isn't the case but rather with an entry point name dist/apps/hello/lambda.js it looks like you're also bundling your application code + dependencies with a bundler like webpack/rollup/esbuild or something?

If so, if you're bundling with a tool you don't need jetpack just use the tool to create a single lambda bundle and completely ignore node_modules and really all files except your output bundle. If not, then I'd need to know more about what your code is doing.

samrith-s commented 3 years ago

Hey, yeah I am using NestJS to bundle everything into my lambda. So it is basically embedding everything into my file, but I just need to ignore these optional modules. I will try with the allowMissing option and let you know.

Thanks for taking the time to respond! :)

ryan-roemer commented 3 years ago

allowMissing only works at the npm package level, so given that you've got dist/../lambda.js outside of node_modules, I don't think it's going to work. We'd have to take a look at re-tooling https://github.com/FormidableLabs/trace-deps to expand allowMissing to include relative application source paths.

ryan-roemer commented 3 years ago

Backing up, do you need to trace dist/apps/hello/lambda.js or does it have all of its dependencies contained already? What actual files have require()s that you need to trace and include other files with?

There's a chance that your packaging solution is just something like:

include:
  - "!**" # exclude everything
  - "dist/apps/hello/**/*.js" # keep all built hello app JS files

without using Jetpack trace mode (instead using either jetpack normal mode (trace: false|unset) or vanilla serverless packaging)

samrith-s commented 3 years ago

It contains all of the dependencies already.

This is my jetpack config:

jetpack:
        collapsed:
            bail: true
        concurrency: 4
        preInclude:
            - '!**'
        trace:
            dynamic:
                bail: true
samrith-s commented 3 years ago

I need to include a particular node module, but exclude certain files. I am at a loss at how to do this.

Also, I set trace to false and still got this error with this config:

jetpack:
    collapsed:
         bail: true
     concurrency: 4
     preInclude:
         - '!**'
     trace: false
ryan-roemer commented 3 years ago

Just leave the trace field off completely and you'll get the same thing. What's your packages.NAME config look like and what is the node_module you need full-path-wise?

samrith-s commented 3 years ago

Hmm, I do not have any packages.NAME config. And I need to include @prisma/cli (but only parts of it) something like this https://github.com/ryands17/serverless-prisma/blob/master/serverless.yml#L33-L36

samrith-s commented 3 years ago

And when I use Jetpack to package, this is my activity monitor: image And this is my terminal: image

Edit: This went on for over 5 minutes, with my laptop's fans blazing with no progress. I am at a complete loss on how to build my app.