Open echocrow opened 7 months ago
Having just faced the same issue, there is a workaround you could use.
my-script.ts
file into a folder, for example scripts
package.json
file to that same directory with {"type": "commonjs"}
You should now be able to run sanity exec scripts/my-script.ts
Add a package.json file to that same directory
I wonder if this would mess with package management/import and/or Typescript config for those script files, assuming a new package.json
adds a new package "scope"?
will be sure to give this a shot next time we need to exec
a script and report back if needed - thanks for the suggestion!
Describe the bug
Sanity CLI provides an
exec
utility command to run a script. This utility command is more convenient than directly executing a script, because it bootstraps some Sanity config, can prime the access token forgetCliClient()
, supports TypeScript files, and more.However, executing a TS file via
sanity exec my-file.ts
errors out whenpackage.json
is configured with"type": "module"
:Strictly speaking, I believe this is not an issue with
sanity exec
per-se. I'm not sure how the CLI executes.ts
files in CJS mode, but a similar (identical?) issue seems to exist ints-node
. Posting this issue here nonetheless.To Reproduce
See Stackblitz Repro with instructions in
README.md
.Summary:
sanity
"type": "module"
inpackage.json
my-script.ts
filesanity exec my-script.ts
Expected behavior
The script executes normally.
Which versions of Sanity / Node.js / npm are you using?
This issue is also present in older Sanity versions and Node 20.
Additional context
Even if this bug is out of scope, maybe there is room to provide an escape hatch in the meantime? That could potentially be:
--loader
flag (or any other Node flag?). (Unsure if that is sufficient to bypass this issue.) orsanity/cli
to programmatically bootstrap Sanity (also exposingwithUserToken
etc. options). This would allow users to execute Sanity scripts however they want (ts-node
,tsx
,vite-node
,bun
(?) etc.).