Hey guys, Is there any examples for setting up sst...
# sst
k
Hey guys, Is there any examples for setting up sst with yarn workspaces in typescript?
t
Shouldn't be anything special
You can ignore the lerna bits if you don't want it
k
@thdxr When I add a my package into the package json in services, it was showing not found error.
t
Can I see the error?
k
@thdxr Here is the error
I have set up a small example where the issue is happening. https://github.com/00karthik/sst-monorepo-poc If you run the project and hit the end point
/trip/app
we will get a 500 error and in the logs we can see that logger is missing
t
When going between packages you cannot import typescript files
You have to build packages/common
So that index.js exists
k
I tried building it. But I could see that that @packages/common in node_modules still has typescript code
t
it should also have js code
let me share a bit more about how I have things setup
k
Yeah that would be awesome 😄
t
Here is my core package - similar to your common package.
My package.json has a an entry
"main": "dist/index.js"
And my
tsconfig.json
looks like this
Copy code
{
  "extends": "@tsconfig/node14",
  "include": [
    "index.ts"
  ],
  "compilerOptions": {
    "declaration": true,
    "outDir": "dist"
  }
}
So when I build, all files go to
dist/
and then when another package imports, it looks for
dist/index.js
k
OMG. It worked. 😮
Thanks alot
Turns out it was the issue with
dist/index.js
in package.json
t
sweet 👍🏽
k
@thdxr Do you build the core package separately ? or does it build when running
sst start
or
sst deploy
?
t
I usually have a watcher on it, it is kind of annoying
Other people configure to import typescript directly via use of aliases but that has other downsides
Sometimes I question why I put it in a separate package to begin with
k
Aha. I see. Thanks.
g
I’m having a similiar problem using the new example you made @thdxr https://github.com/serverless-stack/serverless-stack/tree/master/examples/typescript When execute an api I receive this error:
Copy code
Waiting for the debugger to disconnect...
7b27afef-1906-48c9-81a8-c92c15644c03 ERROR Error: Cannot find module '@linkey/types'
Require stack:
- /Users/GioMaligno/Workspace/linkey/backend/.build/services/stations/index.js
From your example I added a workspace for types (to share type between backend and frontend) as described in the root package.json
"workspaces": ["backend", "types"]
Types tsconfig:
Copy code
{
  "extends": "../tsconfig.json",
  "include": ["index.ts"],
  "compilerOptions": {
    "declaration": true,
    "outDir": "dist",
    "experimentalDecorators": true,
    "baseUrl": ".",
    "paths": {
      "@linkey/types": ["index.ts"],
    },
    "strictPropertyInitialization": false
  }
}
Types package.json:
Copy code
{
  "name": "@linkey/types",
  "version": "0.1.4",
  "scripts": {
    "build": "sst build",
    "script": "esr",
    "test": "jest"
  },
  "dependencies": {
    "class-validator": "^0.13.1"
  },
  "devDependencies": {
    "esbuild-runner": "^2.2.1",
    "jest": "^27.2.0"
  }
}
Backend tsconfig:
Copy code
{
  "extends": "../tsconfig.json",
  "include": ["core", "services", "test", "scripts"],
  "compilerOptions": {
    "declaration": true,
    "outDir": "dist",
    "experimentalDecorators": true,
    "baseUrl": ".",
    "paths": {
      "@linkey/core": ["core"],
    },
    "strictPropertyInitialization": false
  }
}
Backend package.json :
Copy code
{
  "name": "@linkey/backend",
  "version": "0.0.1",
  "scripts": {
    "build": "sst build",
    "script": "esr",
    "test": "jest"
  },
  "dependencies": {
    "@linkey/types": "0.1.4",
    "@types/aws-lambda": "^8.10.83",
    "class-transformer": "^0.4.0"
  },
  "devDependencies": {
    "@types/aws4": "^1.5.2",
    "@types/supertest": "^2.0.11",
    "aws4": "^1.11.0",
    "esbuild-runner": "^2.2.1",
    "jest": "^27.2.0",
    "supertest": "^6.1.6"
  }
}
In my node_module folder @linkey/backend was built on “.build” folder instead of @linkey/types which is never build (no .build folder). It is the first time I use Yarn Workspace I suppose to make some huge mistake 🤔
t
I don't think I would make types its own package
I haven't fully thought through how I'd share types between backend and frontend. I actually do this right now but it's through codegen and it just generates the files in both places
g
I thought to model a types module to keep synchronized models on both side. Did you see drawbacks on this approach?
t
That is a good approach but remember creating a new
package.json
means extra complexity
Have you tried just creating a normal folder with .ts files that you import with
../
If that works then try adding an alias, think making it a real package with package.json isn't needed
I'll play with this myself
g
Yes I could include type folder in backend workspace. For every app I could import types from backend workspace.
I would make some practice with monorepo 😁
t
or in its own folder