https://pulumi.com logo
#typescript
Title
# typescript
f

fresh-spring-82225

02/20/2024, 8:15 PM
How is everyone using private typescript libraries for their pulumi code? I'm struggling with testing changes. I have a single github repo with all my shared iac. Currently I have a github action that uses a tsc and yarn package to transpile the files and send them as a package to a private aws code artifact repo. This works fine, but I don't have a convenient way to test out changes. Ideally I'd like to skip the build process and just point clients temporarily to a branch of the shared iac repo in github. I can use yarn to add a github dependency on my shared iac repo, but the repo doesn't have the transpiled js files. I understand that pulumi does typescript transpilation behind the scenes, so I can just package up the .ts files, but when I try to run pulumi I get the old import error:
Copy code
import * as fs from 'fs'
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
Is there a trick I can use to get pulumi to import .ts files (with es6 module imports) from node_modules? I've tried fiddling with various tsconfig settings, but I haven't found one that works.
l

little-cartoon-10569

02/20/2024, 8:34 PM
There's no need to transpile anyything. Pulumi works with ts-node, so it's all in-memory transpilation. The same works for all JS/TS test libraries. You can package via NPM or workspaces, or a mixture.
If you want ESM in Pulumi code, then put this in your Pulumi.yaml:
Copy code
runtime:
  name: nodejs
  options:
    nodeargs: "--loader ts-node/esm --no-warnings --experimental-specifier-resolution=node"
f

fresh-spring-82225

02/20/2024, 10:47 PM
What's puzzling to me is that pulumi can load an esm typescript file like
shared.ts
in the base project directory without any changes (like
nodeargs
), but if that file is in
node_modules/.../shared.ts
, it fails • I tried adding
nodeargs
as you suggested, but got
Copy code
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../node_modules/ts-node/esm'
• I added
ts-node
as an explicit dependency (to get a newer version than what pulumi depends on), but then got
Copy code
ReferenceError: require is not defined in ES module scope, you can use import instead
when importing pulumi libraries from
shared.ts
....
l

little-cartoon-10569

02/20/2024, 11:03 PM
It's not Pulumi, this is all vanilla TS. You need to get the various targets, modules and so forth all correct.
f

fresh-spring-82225

02/20/2024, 11:23 PM
So many settings 💀 If you (or anyone out there) could point me to a client project that's set up to import a typescript-only library from github this way, that would be greatly appreciated ...
l

little-cartoon-10569

02/20/2024, 11:43 PM
NPM packages usually have js, .d.ts and .map files... don't know about using libraries with .ts files only.
I think you need these in your tsconfig compile optoins:
Copy code
"target": "ESNext",
        "module": "ESNext",
        "moduleResolution": "Node",
And something along these lines in your package.json:
Copy code
"type": "module",
    "module": "esnext",
    "target": "es2020",
f

fresh-spring-82225

02/21/2024, 4:35 PM
yeah, I tried those things and it led to more failures ... giving up on this approach and just doing a file:// dependency
v

victorious-church-57397

02/23/2024, 8:39 PM
What I’ve done for testing changes is building locally and yarn linking the package so I can test before building a new version in the registry
f

fresh-spring-82225

02/23/2024, 10:21 PM
thanks @victorious-church-57397 I haven't tried link yet ... I'm on new yarn, not sure if it works the same
v

victorious-church-57397

02/24/2024, 9:25 AM
Hey, yarn link definitely works for testing local dev packages before publishing them
f

fresh-spring-82225

02/25/2024, 1:34 AM
just tried it with new yarn (yarn berry) and it does work nicely the only drawback is that I still need to run
tsc
after each change to my shared library
v

victorious-church-57397

02/27/2024, 9:08 PM
Yeah you will need to rebuild it when it changes…
3 Views