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.
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.
/usr/bin/env: 'node\r': No such file or directoryForce Unix endings for the script in .gitattributes, and keep the shebang on the first line:
bin/*.js text eol=lf#!/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.