Skip to content
jshacks.dev
Packages

Publish a CLI package with a shebang

A bin script needs a shebang and Unix line endings, or it fails with a confusing env error. Fix CRLF endings with .gitattributes before you publish.

1 min read
Summarize in AI

A CLI that works on your Mac can fail for Linux users with a message that never mentions line endings. If Git on Windows checks the file out with CRLF endings, the shebang becomes #!/usr/bin/env node followed by a carriage return, and the system looks for a program called node\r.

Text
/usr/bin/env: 'node\r': No such file or directory

Force Unix endings for the script in .gitattributes, and keep the shebang on the first line:

.gitattributesText
bin/*.js text eol=lf
bin/cli.jsJavaScript
#!/usr/bin/env node
console.log("ok");

Then reference it from package.json with "bin": { "mycli": "./bin/cli.js" }. npm sets the executable bit on files named in bin when it installs them, so you do not need chmod for the published tarball. The text eol=lf attribute is described in the gitattributes docs.

#!/usr/bin/env node is preferred over a fixed path because Node.js lives in different places on different machines.

Build tools that rewrite the first line, such as a bundler, can drop the shebang. Check the built file, not the source.

Taggednpmcli
Share

More snippets

Packages

Initialize a package with npm init

Initialize a package.json with npm init, skip the questions with the yes flag, and set your own defaults for author and license in npm config.

1 min read
Packages

Generate a changelog from commits

Turn conventional commit messages into a CHANGELOG.md section with conventional-changelog, grouped by feature and fix. Works with any Git history.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.