projen icon indicating copy to clipboard operation
projen copied to clipboard

Unable to add NPM dependency to `AwsCdkConstructLibrary` project // confusing error message

Open yerzhan7 opened this issue 2 years ago • 3 comments

Hi,

I cannot add express NPM library as dependency to my AwsCdkConstructLibrary project.

If this is intended could you please improve error message?

Projen version: v0.56.26

My projen file:

const { awscdk } = require('projen');

const project = new awscdk.AwsCdkConstructLibrary({
  ...
  deps: ['express'],
});

project.synth();

Error message:

[2022-05-21T21:15:10.095] [ERROR] jsii/package-info - Unable to find a JSII dependency named "express" as "^4.18.1". 
If you meant to include a non-JSII dependency, try adding it to bundledDependencies instead.
Error: Error: Error loading /Users/yerzhan/lambda-runtime-upgrader-cdk/node_modules/express/.jsii: Error: 
/Users/yerzhan/lambda-runtime-upgrader-cdk/node_modules/express/.jsii: ENOENT: no such file or directory, open

According to the docs I should be able to do it:

(property) NodePackageOptions.deps?: string[] Runtime dependencies of this module. The recommendation is to only specify the module name here (e.g. express). This will behave similar to yarn add or npm install in the sense that it will add the module as a dependency to your package.json file with the latest version (^). You can specify semver requirements in the same syntax passed to npm i or yarn add (e.g. express@^2) and this will be what you package.json will eventually include. @example [ 'express', 'lodash', 'foo@^2' ] @default [] @featured

yerzhan7 avatar May 21 '22 20:05 yerzhan7

I assume you use it inside a lamdba function and not to export it on your cdk construct interfaces. Therefor add it to devDeps and let NodejsFunction bundle your lambda with express middleware.

pflorek avatar May 27 '22 17:05 pflorek

No, I am not using Lambda function. I cannot simply add any dependency at all to my empty AwsCdkConstructLibrary project.

Nothing works. E.g.:

  deps: [
    'lodash',
  ],

If this is intended could you please update docs and error message? Because your docs say that I should be able to do this.

yerzhan7 avatar May 27 '22 19:05 yerzhan7

It's correct that you can't add express or lodash as dependencies of an AwsCdkConstructLibrary in that way. They are not AWS CDK constructs itself, so you won't export them as an interface of another construct. As the error message states, you should put them in bundledDeps or maybe devDeps. The only use case I can imagine, is using express as runtime dependency, which gets 'compiled' into a lambda or docker container at build time. As long as you don'T export any interface of lodash, it's definitly a bundledDeps, if just used as an internal helper library of your construct. Maybe you can give an example of what you are trying to achieve. For general questions around cdk go to this slack https://cdk.dev/ there are a lot of nice and helpful people

pflorek avatar May 30 '22 17:05 pflorek

Works as designed by jsii. Feel free to open an issue/discussion at aws/jsii.

mrgrain avatar Aug 26 '22 19:08 mrgrain