Search code examples
macosnpxlaunchd

Running Typescript from Launchd


Updated; I found the path to npx using type npx

I would like to run a TypeScript script from a launchd.

I'm successfully running the script directly using /usr/local/bin/npx ts-node /path/to/script.ts

However I can't figure out how to code this into the plist file. I have:

    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/npx</string>
        <string>ts-node</string>
        <string>/path/to/script.ts</string>
    </array>

But this exits with code 127 and logs the error env: node: No such file or directory


Solution

  • Solved this by adding PATH environment variable to the plist file:

        <key>EnvironmentVariables</key>
        <dict>
               <key>PATH</key>
               <string>/usr/local/bin:/bin</string>
        </dict>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/npx</string>
            <string>ts-node</string>
            <string>/path/to/script.ts</string>
        </array>