I have an error in my use of TypeScript modules. A...
# typescript
l
I have an error in my use of TypeScript modules. Apologies for asking here, not sure where I should asking TypeScript questions... From my main index.ts (which imports and uses
@pulumi/pulumi
), I'm calling a function exported from my own module in a different project (brought in using "references": [{ "path": "../mypath/project"}] in my tsconfig.json). That function calls
pulumi.runtime.registerStackTransformation()
. That call produces this error:
Copy code
Error: The root stack resource was referenced before it was initialized.
        at Object.registerStackTransformation (/app/pulumi/node_modules/@pulumi/pulumi/runtime/stack.js:211:15)
        at Object.registerAutoTags (/app/pulumi/autotag/autotag.ts:246:20)
        at Object.<anonymous> (/app/pulumi_stacks/vpc_poc/index.ts:25:11)
        at Module._compile (internal/modules/cjs/loader.js:1133:30)
        at Module.m._compile (/app/pulumi_stacks/vpc_poc/node_modules/@pulumi/pulumi/node_modules/ts-node/src/index.ts:439:23)
        at Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
        at Object.require.extensions.<computed> [as .ts] (/app/pulumi_stacks/vpc_poc/node_modules/@pulumi/pulumi/node_modules/ts-node/src/index.ts:442:12)
        at Module.load (internal/modules/cjs/loader.js:977:32)
        at Function.Module._load (internal/modules/cjs/loader.js:877:14)
        at Module.require (internal/modules/cjs/loader.js:1019:19)
The code is good: when I lift it out of my project/module and put it directly into index.ts it works fine. I'm guessing that this is a scoping issue. Question: how do I call Pulumi code from projects/modules that are outside my application's project? Should I be passing "pulumi" or "pulumi.runtime" from my main module into my referenced project?
g
Can you elaborate on how you're calling this external function?
l
It's an internal library in a separate codebase. The library's tsconfig includes this (don't know what half of it does, I just lifted it from a similar library):
Copy code
"compilerOptions": {
        "composite": true,
        "declaration": true,
        "declarationMap": true,
        "strict": true,
        "target": "es2017",
        "module": "esnext",
        "moduleResolution": "node",
The module in the library just exports a function,
registerAutoTags
The app project's tsconfig includes this:
Copy code
"references": [
        {
            "path": "../pulumi"
        }
And the app's index.ts has this:
Copy code
import { registerAutoTags } from "../../pulumi/autotag/autotag";

// Automatically inject tags.
registerAutoTags({
  "user:Project": pulumi.getProject(),
  "user:Stack": pulumi.getStack(),
  "Env": "Sandbox",
});
(The app's tsconfig is one directory up from index.ts, hence the different paths in references and import.)
g
And you're running this application with the Pulumi cli? e.g.
pulumi up
l
Yep. I'm seeing this with
pulumi preview
. I didn't know there was another way to run it...
g
There isn't. I wanted to make sure you were doing it that way.
l
I'm running Pulumi 2.1.1 via the pulumi/pulumi Docker image.
g
Is
registerAutoTags()
called before all Pulumi resource creations?
l
Yes, it's the first non-import in my index.ts.
Though I do import my other project's module before it.. I'll try moving that import down, see if that makes a difference.
No, same issue. In fact, commenting-out the import of my resource module and my use of it still produces the same issue. All I have left is my registerStackTransformation module and my call to it.
I removed importing pulumi in my autotag module, and instead passed the pulumi I imported in my app code into the
registerAutoTags
function. That works. This reminds me of the issue I had a few days ago where I was using awsx v0.18 with pulumi v2: the lack of peerDependencies in awsx cause a different but similar error.
But I'm using latest everything now. And in this code, only @pulumi/pulumi is used.
Maybe I need to have peerDependencies in my library to get this to work? I did try that, but then I couldn't compile my library because it said nothing was resolving my peerDependencies... 🤔
Ok I've switched all my pulumi dependencies in my library to be peerDependencies, and got it to compile by duplicating them in devDependencies. Deleted my app's node_modules and re-ran
npm install
, just in case.
pulumi preview
is still failing with the same error.