I have found “Execute a JavaScript For Automation” and “Execute Shell script” but I haven’t found a way to require node modules in “JavaScript For Automation” (ReferenceError: Can’t find variable: require) or run node from “Execute Shell script” (line 1: node: command not found) - I have node installed
I haven’t tried this much from a JavaScript for Applications action (except for simple use of js-beautify, see below), but you should get a few pointers from this page:
In particular:
A simple approach for basic cases is to use a shell script action, or from inside a JXA action, use .doShellScript()
I hope you figure this out. I tried to do the same a few months ago, but I just couldn't follow the instructions in the JXA-Cookbook that @ComplexPoint provided.
If you do get it working, would you please post a complete working example here?
It would be a tremendous help.
Oh this is interesting. I had the same question myself, about using node in a plain jxa script that, because I couldn’t figure it out, I solved by upgrading Keyboard Maestro and creating a macro.
How ironic.
Yet, if anyone has the answer for how to do this generally, I’d love to hear it too.
Thanks for sharing, I'm still interesting in this problem.
Can you explain your example a little bit more please? I'm not sure how adjusting PATH should help with running node scripts – I already have node available in my $PATH
You can also run JS scripts as regular shell scripts which is what I've been doing recently, this has the advantage that you can use regular node tooling (sane IDEs, autocomplete, restart script on save) which makes doing development much much easier. Then you just need to pipe the input from KM. Easy peasy lemon squeezy
But how did you solve not having node available as mentioned before?
To make your script work on my machine, I had to adjust first line to not so ubercool #!/usr/bin/env /Users/strajk/.nvm/versions/node/v10.17.0/bin/node
I also use nvm so I have this set in my zshrc right above where nvm is loaded export NVM_SYMLINK_CURRENT=true which makes a symlink to nvm's current version, e.g. the path $NVM_DIR/current/bin is then always pointing to the current node version for me, I did have to expand it out so my ENV_PATH looks like
I've got node 13.8.0 on my machine and you can pass flags in as well to env
top-level await is a new JS thing that's coming soon but it's currently hidden behind a feature flag but you can get rid of the IIFE by using the mjs extension and passing it in --harmony-top-level-await. --no-warnings is needed because node throws a warning saying your using an experimental feature.
I'm getting error permission denied when I try to run my scripts. I'm using nvm and tried to replicate the steps from @ninjakttty ninjakitty's solution but it doesn't work.