5

Using the sam build command I was expecting not to include the aws-sdk package as the Node.js Lambda runtime already includes it.

As I understood the sam build for nodejs is a port of claudia pack command from claudiajs, but I do not see any --no-optional-dependencies flag when I run sam build --help.

I tried installing aws-sdk as an optional dependency but still included.

Is there a way to exclude a dependency from the node_modules directory using the sam build command?

MauricioRobayo
  • 2,207
  • 23
  • 26
  • I had the same issue and made aws-sdk a dev dependency. I think it is only needed for unit testing or calling your code outside of sam local. I see the optional depency included even when I try using claudia pack directly. (It looks like "--no-optional-dependencies" will install a package listed in optionalDependencies as long as package-lock.json is present and it doesn't list the dependency with optional=true. That seems to happen if the optional dependency is actually installed.) – user1170291 Mar 30 '19 at 03:31

2 Answers2

3

From my experimentation I found a couple of options:

  1. Install aws-sdk as a dev dependency
npm i -D aws-sdk
  1. Install aws-sdk as an optional dependency and then use a .npmrc file to disable installing optional decencies on npm install
npm i -O aws-sdk
# .npmrc
optional = false

My folder structure looks something like this:

-- project
   |-- lambdas
   |   |-- lambda1
   |   |   |-- node_modules
   |   |   |   `-- ...
   |   |   |-- .npmrc
   |   |   |-- index.js
   |   |   |-- package-lock.json
   |   |   `-- package.json
   |   `-- lambda2
   |       |-- node_modules
   |       |   `-- ...
   |       |-- .npmrc
   |       |-- index.js
   |       |-- package-lock.json
   |       `-- package.json
   |-- package-lock.json
   |-- package.json
   `-- template.yml

Running sam build in both these instances bundles the packages without the unwanted dependencies for me.

McShaman
  • 3,627
  • 8
  • 33
  • 46
1

McShaman answer is valid for NPM 6.

NPM config has changed in NPM 7 - "optional" was removed.

You should use "omit" instead, to ignore optional dependencies:

https://docs.npmjs.com/cli/v7/using-npm/config#omit

# .npmrc
omit=optional
projected1
  • 11
  • 1
  • 1