obsidian-execute-code: Typescript Not Working
With ts-node path set to /opt/homebrew/bin/ts-node, I get this error:

With ts-node path set to /opt/homebrew/bin/npx ts-node I get this error:
Also tried setting ts-node as an argument
Also tried just ts-node, and settings ts-node as an argument under npx.
Everything works fine from the terminal. Any ideas?
Thanks for a brilliant plugin, I absolutely love this!
Running macOS
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 26 (2 by maintainers)
If you need a quick fix, copy and paste the output of
which nodeinto ts-node path followed by the output ofwhich ts-node. They both need to be in the ts-node path field and the arguments field should be left blank.The problem is that the shell environment executing the provided commands isn’t loading the local
.zshrcor.bashrc. It also only includes bin, /usr/bin, and /usr/sbin. These folders are protected by MacOS and, by default, should not be written to even if it’s just for the purpose of adding symlinks.It would be much better if we could ensure that /usr/local/bin gets added to the shell environment before commands are executed.
@skdamico I haven’t looked through all of the code yet to see how the ts executor is being formed, but I assume that somewhere along the road, a child process is forked off that executes the provided commands. If that’s correct, then the child process only has access to the environmental variables within the scope of the Obsidian process, which isn’t much.
You would need to do one of two things. Either, you would need to source select rc, env, or profile files before executing the provided command or add additional directories to the path yourself. I would recommend the latter.
You would need an input field to accept additional directories, such as what the Shell Commands plugin uses https://publish.obsidian.md/shellcommands/Environments/Additions+to+the+PATH+environment+variable
You would need to concatenate those with the PATH variable and pass that into the child process, the child process would need the additional argument
{ env: dirs.join(":") + ":" + process.env.PATH }. You would of course need to ensure that some dir is provided, otherwise, you wouldn’t pass this argument at all.This would at least allow users to specify which directories they want included in their PATH. For most UNIX users, adding /usr/local/bin would be enough but others may also want to include their homebrew dir, etc.
https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout
Suggest everyone to read the top two answers to this StackExchange. But TL;DR:
.zshrcis for interactive shells and is read once per shell creation.zshenvis for applications, etc and is read every time. export your $PATH, $EDITOR, other application level vars here.ts-nodecalls the same version of node which was used to run it. Now thatwhich nodeworks, could you trynodefor the path and/path/to/ts-nodefor the first argument? e.g:There already were a few issues by people using the combination MaxOS + Homebrew. We don’t have a general solution yet but we there seems to be a common problem.