https://pulumi.com logo
#general
Title
# general
s

salmon-printer-16080

10/18/2021, 9:52 PM
Hey guys, whats the best way to share an
index.ts
file across multiple projects? I want my structure to look like this:
Copy code
--Shared:
Index.ts
package.json

--Project_1:
Pulumi.<dev-stack-name>.yaml
Pulumi.<test-stack-name>.yaml
tsconfig.json

--Project_2:
Pulumi.<dev-stack-name>.yaml
Pulumi.<test-stack-name>.yaml
tsconfig.json
I am running my code in a
pulumi/pulumi-nodejs:3.11.0
container, and my
tsconfig.json
file has the following reference.
Copy code
"files": [
        "../shared/index.ts"
    ]
Pulumi is failing with the following error, so im assuming that it doesnt like referencing an index outside of the project directory..
error: It looks like the Pulumi SDK has not been installed. Have you run npm install or yarn install?
( I have run npm install + npm ci on the shared directory, the node modules look to be installing)
b

billowy-army-68599

10/18/2021, 10:04 PM
hey there. I'm a little lost here, why would you share a single
index.ts
across multiple projects? does it have a componentresource in it?
s

salmon-printer-16080

10/18/2021, 10:11 PM
The index is identical, I am sharing it across projects and using the config files as a degerminator of what to create. That config file has a list of storageAccounts / appServices (along with other services), which the index uses to create those.
b

billowy-army-68599

10/18/2021, 10:13 PM
is there a reason you can't do this with one project, and 4 stacks? instead of 2 projects with 2 stacks in each?
s

salmon-printer-16080

10/18/2021, 10:21 PM
That is a good question and I will look to see if we can do that, however, I am under the impression that we want to keep those separate. In the future there will be 20+ projects, some of which will share the core index.ts file, and some that will require a completely different and more complex implementation.
b

billowy-army-68599

10/18/2021, 10:23 PM
I'm not sure that'll work in the way you intend it to, unless your expect the projects to be different, you should have a single project with 4 stacks. If you want to have reusable code/resources, you should create a ComponentResource and import that into each project
s

salmon-printer-16080

10/18/2021, 10:25 PM
We have some wrappers around some Pulumi resource creation that we are importing into the index.ts currently, is that what you mean, or is ComponentResource a different thing?
Okay just read up on here about it - https://www.pulumi.com/blog/creating-and-reusing-cloud-components-using-package-managers/ We are essentially doing this locally in our project for now, we will look to extend what we have and maybe implement this pattern when the project gets more mature. However, I would like continue exploring if sharing index.ts is possible still so we can make that decision of combining the projects later.