Happy Sunday everybody! Having a bit of a challeng...
# help
d
Happy Sunday everybody! Having a bit of a challenge debugging locally when using a monorepo setup. My repo looks like this:
project
app
server
types
SST is inside the
server
dir,
app
is for my react-native app, `types`is for my typescript types & interfaces that are shared between the two. When I run tests locally, code in
server
can `import { IType } from "@project/types"`successfully. Also, everything works flawlessly when deployed to AWS. However, when I debug a lambda function locally that uses the `import`above, I get the following error: ERROR Error: Cannot find module ‘/Users/davidmartin/dev/myapp/node_modules/@project/types/dist/index.js’. Please verify that the package.json has a valid “main” entry I can debug lambda’s locally when: 1. The lambda does NOT import from
@project/types
2. If I run `npx tsc`and put and
outdir
as “dist” in my tsconfig.json, then dist/index.js is found and debugging works But it feels weird to run `tsc`in my types directory before I debug. I don’t think I should have to do that. I don’t run
npx tsc
at other time. It’s also a bit weird now to have a `dist`directory. Thoughts?
s
I’m having a similar issue, I think. in my root level tsconfig.json, I have:
Copy code
{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@libs/*": ["libs/*"]
    },
and yet, whenever I try to import from
@libs/xyz
in my code, it doesn’t resolve and I get an error.. it’s trying to find an NPM package called
@libs
d
Another problem with this approach: I have to delete that `dist`directory before deploying because I’m getting this error: /Users/davidmartin/dev/myapp/packages/types/dist/index.js 26:4 error ‘DataTypes’ is assigned a value but never used no-unused-vars Fyi, DataTypes is an exported
enum
s
my gut says you definitely don’t want to or need to run
tsc
. but
tsconfig.json
paths
should really work. not sure why it’s not
d
@Sam Hulick I think your issue is different from mine. Looks like you’re using a typescript alias, and I’m importing another package from a monorepo. The difference is that the reference to `@project/types`lives in package.json as:
dependencies: {
"@project/types": "1.0.0"
}
But, I’m also using typescript aliases like you. They work all the time for me, even when debugging lambdas locally. However, my config looks a bit different from yours. In my root level tsconfig.json, I have:
"@lib": ["src/lib"],
"@lib/*": ["src/lib/*"],
Don’t remember why there are two entries, probably something I found on stackoverflow while setting it up. Perhaps try adding the first import similar to mine and see what works? Also, can you share the import statement that doesn’t work?
s
ah sorry, didn’t mean to hijack your thread 😅
d
no worries I’m sure we’ll get around to my issue too. btw, I also have this in my package.json for jest testing: “jest”: { “moduleNameMapper”: { “^@lib/(.*)$“: “<rootDir>/src/lib/$1” }
@Sam Hulick also, btw, my tsconfig.json has a different baseUrl than yours. you might have the correct config for your repo, but worth double checking. here’s mine: “baseUrl”: “.”, “rootDir”: “.”,
s
actually it’s only in VS Code where it complains.
yarn build
works just fine
then again, I’m only importing a TypeScript type. so the build process probably doesn’t care too much. hmm. I’ll have to dig deeper, this is a separate issue
though SST seems buggy too. I import a type from
@nosuchpath/blah
and it builds just fine 😳
d
oh wow, you’re right, it does build just fine that way. i get an error in my editor but not when i build:
s
@thdxr or@Frank ☝️ any thoughts? things are a bit odd here
t
Afk right now but will check in later today
s
no rush at all, I know it’s a weekend 🙂
d
And I think the build issue above is still a different issue from the one I originally posted, which I would like to keep alive in this thread.
f
@Sam Hulick just created an issue for the module not found case https://github.com/serverless-stack/serverless-stack/issues/662
We are going to take a look at it asap.
s
thanks Frank! is this maybe related to
tsconfig.json
paths
not working?
f
Hey Sam, do you mind reposting ur original message: https://serverless-stack.slack.com/archives/C01JG3B20RY/p1628445386244400?thread_ts=1628444037.242300&amp;cid=C01JG3B20RY in the #help channel again as a separate thread? Just makes it easier to dig deeper 😁
s
sure thing!
d
thanks @Frank!
f
Hey @David Martin, just double checking on ur setup:
Copy code
project/
  package.json
  app/
  server/
    package.json
    sst.json
  types/
    package.json
And in `server`’s package.json, you have:
Copy code
dependencies: {
  "@project/types": "1.0.0"
}
And you are seeing
sst deploy
works, but
sst start
doesn’t. Is this right?
d
@Frank yeah for the most part. I simplified it a bit earlier. there’s a package directory. here’s a screenshot.
and yes, in my package.json there is the “@project/types”. again, this configuration is totally working in every case except debugging local lambdas.
f
Do you get the error when you try to start up
sst start
? Or is it on runtime when a requests comes in?
d
btw when I run the debug, i’m starting in VS Code.
yes, the requests do come in
i’m not actually typing
sst start
and doing that works when I run a lambda that does not contain the import
@project/types
and, this also works when i run `npx tsc`in the /types directory after i’ve added an outdir to tsconfig.json
sorry, misread your question --- the error happens during runtime.
f
Got it. And is
@project
an alias, or is
@project/types
the package name for
packages/types
?
d
@project/types is the name of the repo in package.json: “name”: “@project/types”,
it’s not an alias. i do have aliases and they work without issue.
t
does the
types
folder have a
package.json
in it and if so, what is the
main
pointing to?
d
`types`does have a `package.json`and there is a
main
. at first, main pointed simply to “index”, and that failed. i’ve since changed that to point to the file that the error asked for:
dist/index.js
while it pointed to `index`initially, the error still asked for dist/index.ts
i can change it to anything you want and run the debug now
t
If I'm understanding this correctly, the issue is because
@project/types
is being treated as a normal dependency and is expected to be a compiled typescript output. So when code in
server
is looking for it, it's looking for a package.json then looking for the entrypoint. The entry point doesn't exist until you "compile" the package via
tsc
or
tsc -w
if you want to continuously build it. It's likely this doesn't happen in production because importing types don't matter in production. For "shared types" I tend not to treat them like a real package and just import the files from a shared folder
d
Do you consider packages in a monorepo a “normal dependency”? here’s my root package.json: { “name”: “project”, “version”: “0.1.0”, “private”: true, “workspaces”: { “packages”: [“packages/server”, “packages/app”, “packages/types”] } }
(i know i could do packages/* but i have just had issues in the past)
t
Yeah. Imagine if instead of just types, you had actual typescript code in there. You can't import that directly without it being compiled first, you'd have to build it first. Same goes for you even though you're just importing types
I'm not super familiar with monorepo tooling so I'm not sure if there's anything that makes this less painful. I can imagine if you had 10 sub-packages all in typescript it would be annoying to run 10
tsc -w
to setup your environment.
d
i agree that you’ve accurately described the way that `server`looks for
types
through the main entry of
package.json
. let me test the assumption that there should be real code in there that has to be built first. i’m going to export a function that prints hello world.
deploying… fyi, there is a way to not run 10-sub packages. you can run
yarn workspaces build
and that would run the
build
command on all the packages.
t
Nice - wonder how that works with long running commands like
tsc -w
if you're working across several packages
d
i think i know what’s going on
above you said that you need to compile in order to create the dist/index.js in order for that to be found by package.json, so it can be imported by
server
. usually, i would agree with you. but this isn’t the case here. SST is running a packager that’s putting all the JS into a single file, and putting that into the lambda function. so, in this example, in my lambda function, “printHelloWorld” is on line 12,345 (not making that up, it’s just a coincidence). but locally, the configuration must be different.
t
I was just looking to see how esbuild is used locally
At first glance it looks to be used the same way both for local + production build. I am a bit confused as to how esbuild can find compiled code for @project/types or how it knows how to build that project. Unless it's treating it like a module and just importing the ts files directly
d
i think it’s importing the ts files directly. here’s a copy/paste from the lambda function code: // ../types/index.ts var DataTypes; (function(DataTypes2) { DataTypes2[“CAT”] = “CAT”; DataTypes2[“CITY”] = “CITY”; DataTypes2[“CLAIM”] = “CLAIM”; DataTypes2[“COMMENT”] = “COMMENT”; DataTypes2[“FAVORITE”] = “FAVORITE”; DataTypes2[“HOTSPOT”] = “HOTSPOT”; DataTypes2[“IMAGE”] = “IMAGE”; DataTypes2[“IMPORTED_INSTA_POST”] = “IMPORTED_INSTA_POST”; DataTypes2[“INSTAGRAM_CONNECTION”] = “INSTAGRAM_CONNECTION”; DataTypes2[“INSTAGRAM_DATA_DELETION_REQUEST”] = ” INSTAGRAM_DATA_DELETION_REQUEST”; DataTypes2[“INSTAGRAM_SHOPCATS_USER_MAPPING”] = “INSTAGRAM_SHOPCATS_USER_MAPPING”; DataTypes2[“LIKE”] = “LIKE”; DataTypes2[“PLACE”] = “PLACE”; DataTypes2[“POST”] = “POST”; DataTypes2[“SETTING”] = “SETTING”; DataTypes2[“TAG”] = “TAG”; DataTypes2[“USER”] = “USER”; })(DataTypes || (DataTypes = {})); var printHelloWorld = () => { console.log(“hello, world”); };
i mean, they must be transpiled first before importing?
t
It seems like for the production build it's not treated as another package but rather just another file
I can try and recreate this tomorrow and poke around to see what's different about the production build process
d
yeah. i mean, i doubt i’ll be the only person with a monorepo who wants to debug so it’s probably worthwhile. and i don’t think there’s anything unique or wrong with my project configuration.
if you want, i can add you to my github repo so you can play with it.
t
Weirdly enough I think it's the production behavior that's "wrong" (even though it's desirable). Yeah adding me would be helpful my username is thdxr on github
d
but yeah, enjoy your sunday! this isn’t urgent because I have a temporary workaround, but it’s definitely part of the appeal of SST.
ok, adding you. hope you like cats 🙂
t
Yeah definitely need to fix this
haha cool thanks