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

Npm 7 workspace monorepo packaging #187

Closed
leemhenson opened this issue Jan 28, 2021 · 3 comments
Closed

Npm 7 workspace monorepo packaging #187

leemhenson opened this issue Jan 28, 2021 · 3 comments

Comments

@leemhenson
Copy link

Hi. I know there's the open PR regarding lerna monorepos using symlinks (#142) but I'm currently working with a different arrangement which uses npm v7 workspaces, typescript project references, a flat directory structure and relative paths, e.g.:

/package.json
/node_modules   # <--- all dependencies get hoisted here, including the symlink to the /dist
/packages
  /common-a
    /package.json
    /dist/index.js
    /src/index.ts
  /common-b
    /package.json
    /dist/index.js
    /src/index.ts
  /function-1
    /package.json
    /dist/handler.js
    /src/hander.ts

Imports inside the handler, after compilation, look like require("../../../common-a/dist/index"). So, this is a path which escapes the confines of what serverless considers the servicePath. When jetpack produces the zip, it all looks great, except the handler paths are missing the necessary parent segment ("function-1") to make that relative path work:

common-a/dist/index.js
common-b/dist/index.js
dist/handler.js

I guess I'm looking for a way to say "prepend the function's file paths in the zip with 'function-1'". Is there any prior art in this sort of area?

Cheers!

@leemhenson leemhenson changed the title Non-symlinked monorepo packaging Npm 7 workspace monorepo packaging Jan 28, 2021
@ryan-roemer
Copy link
Member

What does your serverless.yml look like and which directory are you running the serverless commands from?

@leemhenson
Copy link
Author

Hey Ryan, I found a reasonable solution:

  1. put all my serverless.ymls alongside the root node_modules
  2. name the serverless.ymls individually, e.g. serverless.app-1.yml
  3. fully qualify the paths inside those config files, e.g. handler: packages/app-1/dist/x/y/z/handler.handler
  4. use sls -c serverless.app-1.yml <other stuff>

Ideally i'd like to namespace the .serverless directories too, but that doesn't look possible right now. Either way, the serverless-jetpack is doing the biz exactly as I want now. And serverless-offline is happy with this arrangement too.

I don't need that other branch I made where I changed the trace algo to track nested node_modules + symlinks. That was a suboptimal monorepo layout anyway, as it turns out.

@ryan-roemer
Copy link
Member

ryan-roemer commented Jan 29, 2021

Glad you found a solution!

Yeah, I think the root of the problem is maybe this: https://github.com/FormidableLabs/serverless-jetpack/#packaging-files-outside-cwd which is that a zip file considers CWD "the root" and just collapses everything else into place there. Typically this isn't too bad of a deal because of luck in how require() resolution works for node_modules (and your case is I think hitting it because you have application sources that don't get magically resolved to one of many directories), but it's always safest to be have CWD be the absolute root of where any code (node_modules or app sources) can be found...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants