Hey guys. I’m lost on this one, it’s really hard f...
# general
a
Hey guys. I’m lost on this one, it’s really hard for me to understand the differences between
commonjs
and
esnext
that some frameworks uses on their TS Config file. I created my own private construct library to host some constructs, and I never do imports with
.js
. Copied the
esnext
config from SST source code, and my library is failing due this issue… it can not find my imports. TL;DR: why SST usses
esnext
, appending
.js
on everything, and not
commonjs
? Which one should I use?
t
Yeah this confusiok is going to continue because of this awkward phase we're in
CommonJS is old and shouldn't be used anymore
ESM needs file extensions
Because it's a unified import system between browser and nodejs
a
I see.
This is what drives me crazy about Node/JS/TS stack.
t
Yeah we're almost in a good place
a
There are so many ways to do things… and all are supposed to work together…
t
Finally cleaning everything up but it's going to be painful
a
At this point, I don’t know how to make my project work.
t
I need to write up my ESM guide
a
I’m getting this error on my Project: SyntaxError: The requested module ‘@securitize/cdk-website’ does not provide an export named ‘WebsiteDeployment’
Copy code
import { WebsiteDeployment } from "@securitize/cdk-website";
When importing my own lib.
So I’m using CommonJS, but looks like it doesn’t like my exports.
t
You need to add an exports field to package.json
a
My question is… IF -> I’m building my own lib project IF -> I’m using it on my own infra project What config should I use… in both projects.
t
You can't use CommonJS with sst, use ESM everywhere
a
Ok.
So I need to use
.js
in the imports?
t
When writing an ESM library you need to 1. Set type: "module" in package.json or name everything .mjs 2. Add an exports field pointing to index 3. All import statements should have file extensions Then you can import as normal in your stacks code
a
Ok I see.
Actually, I think the problem was my `index.ts`:
Copy code
export * from './WebsiteDeployment.js';
I added
.js
there now.
Going to try again.
@thdxr thanks, that worked.
I hate adding the extension.
Still thinking it’s too complicated to configure node properly.
t
I agree it took me a long time to memorize all of this
a
Yeah, I need to find a guide for this, to wrap my head around this stuff.
t
I really have to write it, maybe will make it tomorrow's project
a
And I didn’t mention the
target
and
lib
.
Every time I open the
tsconfig
file I want to kill myself.
I never understand what I’m doing.
Hhahahahaa.
a
You can't use CommonJS with sst, use ESM everywhere
What? Since when? I only use CommonJS because ESM is such a minefield.
t
You require resources from sst?
Copy code
const { Api } = require("@serverless-stack/resources")
a
No, TypeScript import but is that not transpiling to require?
t
It used to but not anymore since we wanted to get optional dependencies working well and that needed top level await
It shouldn't effect anything unless you had
@serverless-stack-slack/resources
as a dependency in an environment that cannot tolerate esm deps
a
Oh. So it's overriding tsconfig and package.json, and you're just talking about the stacks, not my Lambda code?
t
yeah just stcks code which shouldn't matter for most people
a
@thdxr I’m trying Turbo now… But I have problems with dependencies… I’m using Storybook, and it has a dependency on
@mdx-js/react
old version. But some other app is bulling the latest.
So for some reason Turbo is not allowing me to support 2 versions of the same lib.
Is this possible?
Actually… looks like in the project
node_modules
it has the right version… but when it builds, it’s uses the one on the root
node_modules
😐