How is everyone using private typescript libraries...
# typescript
f
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
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
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
It's not Pulumi, this is all vanilla TS. You need to get the various targets, modules and so forth all correct.
f
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
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
yeah, I tried those things and it led to more failures ... giving up on this approach and just doing a file:// dependency
v
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
thanks @victorious-church-57397 I haven't tried link yet ... I'm on new yarn, not sure if it works the same
v
Hey, yarn link definitely works for testing local dev packages before publishing them
f
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
Yeah you will need to rebuild it when it changes…