Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot find module 'aws-cdk/lib/api/aws-auth' when using CDK installed with homebrew #90

Closed
AllanOricil opened this issue Feb 27, 2024 · 8 comments · Fixed by #92
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@AllanOricil
Copy link

AllanOricil commented Feb 27, 2024

It does not work with cdk installed via homebrew

image

The required libraries should be fetched after determining the CDK installation, as people could have not installed it with npm.

You could also try to publish this cli to the homebrew registry. This way you can require homebrew's global node modules, I think

image
@AllanOricil AllanOricil changed the title Error: Cannot find module 'aws-cdk/lib/api/aws-auth' Error: Cannot find module 'aws-cdk/lib/api/aws-auth' when using CDK installed with homebrew Feb 27, 2024
@MarcelStranak MarcelStranak added the bug Something isn't working label Mar 6, 2024
@lakkeger lakkeger removed the bug Something isn't working label Jun 27, 2024
@lakkeger
Copy link
Contributor

Hi @AllanOricil!
Please set your NODE_PATH to the node_module folder of your Brew installed AWS CDK and it will work.
In my case this would be something like this:
export NODE_PATH=/opt/homebrew/Cellar/aws-cdk/2.147.1/libexec/lib/node_modules
For further information please have a look at NodeJS documentation on module loading: https://nodejs.org/api/modules.html#loading-from-node_modules-folders
Hope this helps. If you have any further questions please let us know.

@AllanOricil
Copy link
Author

@lakkeger if I do it, NVM will probably break. I use multiple node versions, in multiple projects, so I can't break NVM

@lakkeger
Copy link
Contributor

lakkeger commented Jun 27, 2024

The issue you experience in general not a bug in aws-cdk-local but a module look up issue with NodeJS. Our assumption by default is that node knows well where to look up the necessary packages, in this case aws-cdk. Since you've installed it with brew this is not the case.
So as a solution I see 3 options:

  1. We could pin to certain aws-cdk version as a hard dependency to the project but that would result in inflexibility so you'd need to rely to a certain version always to use a new feature in CDK and your brew install would be ignored.
  2. Alternative option would be to process your environment and look up your CDK installation, but that comes with many assumptions and locations where the lib could be found. ie brew installed lib not living under the same folder structure as the node one
  3. We can rely on NodeJS provided option, the NODE_PATH. According to the docs by defining NODE_PATH you expand the lookup path not overriding it:

If the NODE_PATH environment variable is set to a colon-delimited list of absolute paths, then Node.js will search those paths for modules if they are not found elsewhere.

ref: https://nodejs.org/api/modules.html#loading-from-the-global-folders
As you can tell from the above quote NODE_PATH is the last place nodejs will look your module.

So in context of the above I see no value in it to implement something that we can work around with a small config change.

If you are still worried, I'd recommend to use something like direnv/nix/devcontainers to separate project environments completely not just on the node level so your NODE_PATH can be unloaded/loaded by every project. (Perhaps the easiest to implement it without big changes is direnv.)

@lakkeger lakkeger added the wontfix This will not be worked on label Jun 27, 2024
@AllanOricil
Copy link
Author

AllanOricil commented Jun 27, 2024

@lakkeger why not print a message on the terminal to guide users to update NODE_PATH? The main issue is that I got lost and had no directions, and had to wait weeks for you to help me

"we identified cdk packages are not installed in the same directory as localstack. Please, update NODE_PATH with the path where cdk modules are installed running the following command ..."

@lakkeger
Copy link
Contributor

I understand your frustration and our team is working on it tirelessly to improve the product and the UX/DX of the surrounding ecosystem. Unfortunately there was only best effort support and development on this repo for a long time.
As a reasonable middle ground I can offer the following:

  • we can throw a better exception if the aws-cdk module is missing, perhaps do an additional OS check so we can ask if you've added your CDK installation to the NODE_PATH
  • additionally I can add this particular scenario as a MacOS specific section to the README so there is some guidance about this tooling combination

@lakkeger lakkeger added documentation Improvements or additions to documentation enhancement New feature or request and removed wontfix This will not be worked on labels Jun 28, 2024
@lakkeger lakkeger linked a pull request Jul 1, 2024 that will close this issue
@rdingwell
Copy link

Just to add onto this error, it's not just related to a homebrew install. I have the same issue having installed cdk-local with npm.
npm install -g aws-cdk-local aws-cdk

@lakkeger
Copy link
Contributor

Hi @rdingwell,
Please provide some information on the packages, steps and the system you're running into this issue as I was unable to reproduce it with latest versions.

@rdingwell
Copy link

@lakkeger I found my issue when looking through my .npm directory. You can install cdk in a couple of ways using npm, either with npm install -g aws-cdk or npm install -g cdk . Using cdk, it installs the aws-cdk library under it's own node_modules directory which makes it unaccessible to other libraries. Once I removed the cdk node module and installed via aws-cdk everything worked fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants