Using NPX command for shell script shebang / interpreter
Asked Answered
L

2

8

I'd like to run a command line script using the coffee executable, but I'd like to call that executable through npx.

Something like #!/usr/bin/env npx coffee does not work, because only one argument is supported via env.

So, is there a way to run an npx executable via env?

Long answered 20/4, 2019 at 20:58 Comment(2)
Commenting as it seems relevant, but doesn't seem to qualify as an answer... According to this Unix & Linux Stack Exchange answer, starting with coreutils 8.30 you'll be able to use the -S option to pass multiple arguments. Works on MacOS now with a shebang for TypeScript like: #!/usr/bin/env -S npx ts-node --compiler-options {"module":"commonjs"} Unfortunately does not yet work with Debian Buster :(Kiaochow
My current understanding is that node inserts a "shell"/"cmd" shim whenever it detects a shebang for npx packages. From the cmd shim, it accepts multiple arguments. It's not documented anywhere I know of, but it's in the code: github.com/npm/cmd-shim/blob/…Pessimism
W
1

Here is a solution using ts-node.

Any single OS, ts-node installed globally

#!/usr/bin/env ts-node

// TypeScript code

You probably also need to install @swc/core and @swc/cli globally unless you do further configuration or tweaking (see the notes at the end). If you have any issues with those, be sure to install the latest versions.

macOS, ts-node not installed globally

#!/usr/bin/env npx ts-node

// TypeScript code

Whether this always works in macOS is unknown. There could be some magic with node installing a shell command shim (thanks to @DaMaxContext for commenting about this).

This doesn't work in Linux because Linux distros treat all the characters after env as the command, instead of considering spaces as delimiting separate arguments. Or it doesn't work in Linux if the node command shim isn't present (not confirmed that's how it works, but in any case, in my testing, it doesn't work in Linux Docker containers).

This means that npx ts-node will be treated as a single executable name that has a space in it, which obviously won't work, as that's not an executable.

See the notes at the bottom about npx slowness.

Cross-platform with ts-node not installed globally in macOS, and some setup in linux

Creating a shebang that will work in both macOS and Linux (or macOS using Docker running a Linux image), without having to globally install ts-node and other dependencies in macOS, can be accomplished if one is willing to do a little bit of setup on the Linux/Docker side. Obviously, Linux must have node installed.

  1. Use the #!/usr/bin/env npx ts-node shebang. We just have to fool Linux into thinking that npx ts-node with the space is actually a valid executable name.

  2. Build a named Docker image that has the required dependencies globally installed and a symbolic link making npx ts-node resolve to just ts-node.

    Here is an example all-in-one command line on macOS that will both build this image and run it:

    docker buildx build -t node-ts - << EOF
      FROM node:16-alpine
      RUN \
        npm install -g @swc/cli @swc/core ts-node \
        && ln -s /usr/local/bin/ts-node '/usr/local/bin/npx ts-node'
      ENV SWC_BINARY_PATH=/usr/local/lib/node_modules/@swc/core/binding
      WORKDIR /app
    EOF
    
    docker run -it --rm \
      -v "$(pwd):/app" \
      node-ts \
      sh
    

    Note that for this example script to work, the above line containing EOF must not have any other characters on the line, before or after it, including spaces.

  3. Inside of the running container, all .ts scripts that have been made executable chmod +x script.ts will be executable simply by running them from the command line, e.g., ./test-script.ts. You can replace the above sh with the name of the script, as well (but be sure to precede it with ./ so Docker knows to run it as an executable instead of pass it as an argument to node).

Additional Thoughts & Considerations

There are other ways to achieve the desired functionality.

  • The docker run command can mount files into the image, including mounting executables in various directories. Some creative use of this could avoid needing to install anything or build a docker image first.
  • The install commands could be part of the docker run instead of pre-building an image, but then would be performed on each execution, taking much longer.
  • The PATH could be modified in macOS, linux, and in the docker build to add the folder containing ts-node's bin.js from any ts-node dist directory, then a shebang of #!/usr/bin/env bin.js should theoretically work (and can try bin-esm.js to avoid needing SWC, though this enters experimental node territory and may not be suitable for production scripts). This works in macOS, and in Docker outside of an npm project, and in Docker inside of an npm project configured to use TS & swc by passing the --skipProject flag to ts-node or setting environment variable TS_NODE_SKIP_PROJECT=true. A working test command line example: docker run -it --rm -v "$(pwd):/app" -e TS_NODE_SKIP_PROJECT=true -w /app --entrypoint sh node:16-alpine -c 'PATH="$PATH:/app/node_modules/ts-node/dist" ./test.ts'.
  • Any named executable that can be found in the PATH and run via direct command can be a shebang (using #!/usr/bin/env executable). It can be a shell script, a binary file, anything. A shell script can easily be put at a known location, added to the PATH, and then call whatever you like. It could be multi-statement, compiling the file to .js, then running that. Whatever your needs are.
  • In some special cases you might want to simply use node as your shebang executable, setting node options through environment variables to force ts-node as your loader. See ts-node Recipes:Other for more info on this.

Notes:

  • The SWC_BINARY_PATH environment variable ensures that ts-node can find the architecture-specific swc compiler (to avoid error "Bindings not found"). If you're running on only one architecture, you won't need it. Or, if you are mounting node_modules that have these @swc packages already installed for the correct architecture, you won't need it.
  • It is possible to install node_modules binaries for multiple architectures. The way to do this varies between the different package managers. For example, yarn 3 lets you define which binaries to install all at once in .yarnrc.yml. There are other options for npm and possible yarn 1 (and 2?) using environment variables.
  • ts-node does offer options for running without swc (though this is slower). You could try shebangs with ts-node-esm instead of ts-node. Look at all the symlinks in the /usr/local/bin folder or consult ts-node documentation for more information. If ts-node
  • It is possible to run .ts files directly using node and setting node options in environment variables. node --loader=ts-node does work, in recent versions (16+?). The experimental mode warnings can be suppressed.
  • There are some crazy ways to trick the shell to run JavaScript instead of a unix shell. Check out this answer that uses a normal sh shebang, but a clever shell statement to transfer execution over to node, that is basically ignored by JavaScript. This isn't great as it requires extra lines of trickery, but could help some people. Other answers on the page are also instructive and it's worth reviewing to get the full picture.
  • Some of the complexity here might go away if running .ts files outside of an npm project. In my own testing in Docker, the context was always in a project having its own tsconfig.json and swc installed, so with a different setup you might have different results. It proved to be difficult to get ts-node to ignore npm project context found with the executed .ts file.
  • The difference between ESM and CommonJS module handling has not been explained here. This is a complicated topic and beyond the scope of this answer.
  • Suffice it to say that if you can figure out how to run your scripts from the command line in the form executable [options] [file], then you should be able to figure out how to run ./[file] with an appropriate shebang, by mixing and matching all the ideas presented here. You don't have to use ts-node. You can directly use node, swc, tsc itself (by first compiling and then running any .js file or set of .js files in the found context), or any utility or tool that is able to compile or run TypeScript.

Note that using npx is significantly slower than running ts-node directly, because it may need to download the ts-node package, and dependencies, every time it runs.

Some various random tips on possible strategies for SWC architecture support:

Woodbridge answered 29/12, 2022 at 6:26 Comment(0)
P
1

You can use the -S option of env:

#!/usr/bin/env -S npx tsx

// TypeScript code

I can confirm this work on:

  • env (GNU coreutils) 9.5 on MacOS 14
  • env (GNU coreutils) 9.5 on Arch Linux
  • env (BSD) on MacOS 14
Parquetry answered 12/4, 2024 at 22:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.