Hi there! I'm trying to setup a new pulumi project...
# typescript
e
Hi there! I'm trying to setup a new pulumi project using typescript and, from the docs, I gather that exporting an async function that returns the desired output should be sufficient. However, running that pulumi program has no effect. If I export the promise resulting from calling that function, it does work. Are the docs wrong or am I missing something?
m
I just tried this with a new
aws-typescript
project (based on the doc you linked), and it seems to work for me:
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

export = async () => {
    const bucket = new aws.s3.Bucket("my-bucket");

    return {
        bucketName: bucket.id,
    };
}
Output:
Copy code
➜ pulumi up
Previewing update (dev)

     Type                 Name                        Plan       
 +   pulumi:pulumi:Stack  aws-typescript-cfe895a-dev  create     
 +   └─ aws:s3:Bucket     my-bucket                   create     

Outputs:
    bucketName: output<string>

Resources:
    + 2 to create
Does your code look different?
The
return
object is optional by the way -- they docs don't make this super clear (we'll fix that)
e
Interesting! The only difference is that I have
export default
instead. I'm not familiar with the
export =
syntax.
That one does work but I get a warning from
tsserver
:
I guess
export =
is commonjs vs
export default
being esm?
m
More or less, yeah.
export =
is a TypeScript thing for use with CommonJS. It's essentially like saying
module.exports = { your: stuff }
(and indeed compiles to that in JS). https://www.typescriptlang.org/docs/handbook/modules/reference.html#export--and-import--require
e
Ah, perfect. So this is indeed a type of export that is impossible with ESM, hence the warning. I guess there's something in my tsconfig tricking tsserver into thinking it's targeting esm. I'll look into that. Thanks for the clarification, that taught me something new!
m
You bet! Thanks for asking and glad that helped. Check your
tsconfig.json
and make sure it's not
"type": "module"
-- that could be it.
👍 1
e
Btw, after changing the code to use
export =
, I noticed the following message when a stack update fails:
Copy code
error: update failed

    The Pulumi runtime detected that 171 promises were still active
    at the time that the process exited. There are a few ways that this can occur:
      * Not using `await` or `.then` on a Promise returned from a Pulumi API
      * Introducing a cyclic dependency between two Pulumi Resources
      * A bug in the Pulumi Runtime
    Leaving promises active is probably not what you want. If you are unsure about
    why you are seeing this message, re-run your program with the `PULUMI_DEBUG_PROMISE_LEAKS`
    environment variable. The Pulumi runtime will then print out additional
    debug information about the leaked promises.
I wonder if this is due to the fact I'm exporting an async function or maybe it has to do with the usage of promises as inputs to other resources.