David Martin
08/08/2021, 5:33 PMproject
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?Sam Hulick
08/08/2021, 5:56 PM{
"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
David Martin
08/08/2021, 6:04 PMenum
Sam Hulick
08/08/2021, 6:12 PMtsc
. but tsconfig.json
paths
should really work. not sure why it’s notDavid Martin
08/08/2021, 6:15 PMdependencies: {
"@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?Sam Hulick
08/08/2021, 6:17 PMDavid Martin
08/08/2021, 6:18 PMDavid Martin
08/08/2021, 6:20 PMSam Hulick
08/08/2021, 6:21 PMyarn build
works just fineSam Hulick
08/08/2021, 6:22 PMSam Hulick
08/08/2021, 6:23 PM@nosuchpath/blah
and it builds just fine 😳David Martin
08/08/2021, 6:25 PMSam Hulick
08/08/2021, 6:26 PMthdxr
08/08/2021, 6:35 PMSam Hulick
08/08/2021, 6:36 PMDavid Martin
08/08/2021, 6:52 PMFrank
Frank
Sam Hulick
08/08/2021, 10:17 PMtsconfig.json
paths
not working?Frank
Sam Hulick
08/08/2021, 10:24 PMDavid Martin
08/08/2021, 10:27 PMFrank
project/
package.json
app/
server/
package.json
sst.json
types/
package.json
And in `server`’s package.json, you have:
dependencies: {
"@project/types": "1.0.0"
}
And you are seeing sst deploy
works, but sst start
doesn’t. Is this right?David Martin
08/08/2021, 10:30 PMDavid Martin
08/08/2021, 10:31 PMFrank
sst start
? Or is it on runtime when a requests comes in?David Martin
08/08/2021, 10:35 PMDavid Martin
08/08/2021, 10:35 PMDavid Martin
08/08/2021, 10:35 PMsst start
David Martin
08/08/2021, 10:36 PM@project/types
David Martin
08/08/2021, 10:36 PMDavid Martin
08/08/2021, 10:37 PMFrank
@project
an alias, or is @project/types
the package name for packages/types
?David Martin
08/08/2021, 10:41 PMDavid Martin
08/08/2021, 10:41 PMthdxr
08/08/2021, 10:42 PMtypes
folder have a package.json
in it and if so, what is the main
pointing to?David Martin
08/08/2021, 10:43 PMmain
. 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
David Martin
08/08/2021, 10:44 PMDavid Martin
08/08/2021, 10:44 PMthdxr
08/08/2021, 10:47 PM@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 folderDavid Martin
08/08/2021, 10:50 PMDavid Martin
08/08/2021, 10:50 PMthdxr
08/08/2021, 10:51 PMthdxr
08/08/2021, 10:51 PMtsc -w
to setup your environment.David Martin
08/08/2021, 10:53 PMtypes
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.David Martin
08/08/2021, 10:57 PMyarn workspaces build
and that would run the build
command on all the packages.thdxr
08/08/2021, 10:59 PMtsc -w
if you're working across several packagesDavid Martin
08/08/2021, 11:02 PMDavid Martin
08/08/2021, 11:04 PMserver
. 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.thdxr
08/08/2021, 11:05 PMthdxr
08/08/2021, 11:09 PMDavid Martin
08/08/2021, 11:11 PMDavid Martin
08/08/2021, 11:11 PMthdxr
08/08/2021, 11:12 PMthdxr
08/08/2021, 11:12 PMDavid Martin
08/08/2021, 11:14 PMDavid Martin
08/08/2021, 11:14 PMthdxr
08/08/2021, 11:15 PMDavid Martin
08/08/2021, 11:16 PMDavid Martin
08/08/2021, 11:16 PMthdxr
08/08/2021, 11:16 PMthdxr
08/08/2021, 11:16 PM