Hey everyone, has anyone worked out how to use a t...
# help
r
Hey everyone, has anyone worked out how to use a top level
await
yet? Getting this error and I cant figure out how to resolve it even though node14 in lambda supports it.
Copy code
[ERROR] Top-level await is not available in the configured target environment ("node14")
Example usecase is the graphql demo but building federation into apollo :
Copy code
const federatedSchema = await buildFederatedSchema({
  typeDefs: gql(printSchema(schema)),
  resolvers: createResolversMap(schema) as any,
});
https://github.com/serverless-stack/serverless-stack/tree/master/examples/graphql-apollo
m
It's a challenge, but yeah. You need to set the bundling options to
format: OutputFormat.ESM
and
target: esnext
, among a few other gotchas.
r
ah thanks
Copy code
app.setDefaultFunctionProps({
  bundle: {
    format: "esm",
  },
});
seems to have worked
m
In that case looks like the function bundling is all abstracted away, so you wouldn't be able to do it unless there's a way to pass in esbuild config.
That could be it!
r
Thanks Matt 👍
m
np
t
Yep we supported esm a few months ago!
top level await is critical for SSM loading
m
Not sure if you ran into any issues with dependencies, but I had to use the hack above for some version of aws-sdk.
(bundling in for speed instead of using the provided one)
t
Yep we built that hack into sst, was making me go crazy until that guy posted the fix
@Ross Gerbasi was the one who opened that issue
m
Ah cool
r
@thdxr i have enabled esm as above but i am now getting error
Copy code
"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"ReferenceError: __dirname is not defined",
which is coming from
.build/run.js
any ideas what would cause that?
t
I think you currently need to have your esm code in a subpackage
r
__dirname
doesnt exist when its
esm
i get that but i dont actually have anything in my own code its just in the build output
offending lines:
Copy code
// Check first and throw an error
if (!fs.existsSync(path.join(__dirname, "lib", "index.js"))) {
  console.error(
    `\nCannot find app handler. There was a problem transpiling the source.\n`
  );
  process.exit(1);
}
t
Yeah I'm saying you need to have a mono repo setup currently because sst CLI does some non esm stuff
r
oh i see
ironically just moved this out of a monorepo for testing lol
t
haha yeah we'll fix this soon, next big project is improving a bunch of little CLI stuff
r
cool thanks for the help much appreciated!
just and FYI @thdxr i moved this back into the monorepo and it still fails. Not a major issue right now for me as I have switched back from usinng await at top level.
t
what is the error when it fails?
here's a sample stack where it works: https://github.com/serverless-stack/graphql-stack note backed/package.json is type: "module" but root package.json is not
r
i was getting the error
Copy code
"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"ReferenceError: __dirname is not defined",
i can take another look over the weekend compare it against your repo thanks
t
ah that means some code of yours (or a library) is trying to use
__dirname
which isn't allowed in esm
r
well thats the issue, the __dirname is coming from the CLI 🙂