I am trying to start my cli tool via the package.json bin
property.
I have the following:
...
"name": "mycli",
"bin": "./bin/mycli",
...
When I open the cmd in the package path and type: "mycli" it says that the command is not recognized.
Should I run an npm command? or use the scripts property? am I trying to access the bin property incorrectly?
Answer from Rodrigo Medeiros works for me, but only if I have too the shebang line at the .js file.
There I had another issue. I have node.js installed at c:\Program files\nodejs, and this was my shebang line:
This didn't work, because the blank space. This was the correct one:
Whenever I was trying to get my app to link, I kept running into problems on Windows where the generated scripts that would execute on path would try to run the
*.js
file using the default Windows executable (I don't know what that would be). I'm not sure why. I think it might be because it is a JavaScript file. However, I compared the generated scripts to some of the other modules I had installed, and figured out that if I made thebin
file referenced by thepackage.json
act as though it were to be executed on a*nix
machine,npm
would automatically try and add the call to node.For example:
If my
package.json
looks like this:myapp/package.json
My referenced bin file looks like this:
myapp/bin/myapp
The 2 generated executable files that appear in
%APPDATA%\npm
show up as follows by running the commandnpm link
from within themyapp
directory (which would havepackage.json
in the root):myapp
myapp.cmd
Bear in mind, I didn't need to make the 2 files above explicitly, I just needed to have the file to be executed as the
bin
file in thepackage.json
.npm
did the file creation.Line Endings
One other thing to note that I ran into while using this method, make absolutely sure that your line endings are correct. I noticed that my bin was erroring with: ": No such file or directory" whenever I installed on *nix machines because there was an incorrect line ending. Thanks to View line-endings in a text file for example of how to print visible line endings.
For example, if you run
cat -e PATH_TO_BIN
and get something like this:You're using the wrong line endings. If you get something like this:
Those should be the right line endings.
Try to specify the name of your
cli
tool in thebin
property, like:Then, run
npm link
, from inside your project folder, to create a global symbolic link to the current folder.Don't forget to add the
"preferGlobal": "true"
property just before thebin
property in yourpackage.json
file, in order to warn users to install your module globally.If you put
in the first line of your script, npm will create the necessary wrapper scripts.