I'm trying to create a command line tool with node (following these instructions).
So I add "bin" to package.json file
"bin": {
"test": "./index.js"
}
Created index.js file
#!usr/bin/env node
console.log('test');
And I use $ sudo npm link.
Finally I try to run it via terminal but I'm getting -bash: /usr/local/bin/test: usr/bin/env: bad interpreter: No such file or directory
Any ideas anyone?
I don't think npm link is needed in this case; you normally would use it to connect a module from your file system that you might otherwise pull in via the npm install command.
Your shebang syntax looks off to me, though, missing the leading slash:
#!/usr/bin/env node
npm link links the module locally. For your bin, it means that the binary is installed into node_modules/.bin/ (if i recall fine).
To link a package globally, you should use the --local arg.
npm link . --local
Related
I ran the following command for my node app:
$ npm install browser-sync --save-dev
Installation was successful, browser-sync appears in my package.json file as well as my node_modules directory.
However, when I run $ browser-sync --version to check that it's working, I get the following error:
bash: browser-sync: command not found
Why isn't this working?
Note: this question is similar, but I don't want to have to install it globally as in this question.
Any help would be greatly appreciated!
This is because you're trying to use a module locally which is normally installed globally. Modules installed globally end up on your PATH environment variable, which is why you can run them from the terminal as you're trying to do:
$ browser-sync --version
If you want to use the browser-sync module from a local install you will have to prepend the full path to the browser-sync binary from within your .bin directory since all locally installed modules are placed within your current working directory node_modules directory. i.e. Node modules go in ./node_modules, executables go in ./node_modules/.bin/. So in order to run the browser-sync binary from a local install do the following:
./node_modules/.bin/browser-sync --version
Hopefully that helps!
If you installed browser-sync using npm --save or npm --save-dev you can run it by writing a script in your package.json. Here's an example of a script I added:
{
...
"scripts": {
"dev-server": "browser-sync start --server 'public' --files 'public'"
},
...
}
You can run the scripts from you project's root directory like so
npm run dev-server
This will run whatever command is set to dev-server in your script. In this case it will run browser-sync for the app/site in a folder called /public and watch for any file changes in the /public folder. I know this question is a bit old but it was unanswered and hopefully I can save someone time in the future.
The other answers still work, but a newer approach has emerged since npm added the npx command: npx <package-name>.
This command allows you to run an arbitrary command from an npm
package (either one installed locally, or fetched remotely), in a
similar context as running it via npm run.
Source: https://docs.npmjs.com/cli/v8/commands/npx
In this case, you would run npx browser-sync.
I read on the npm documentation that you can't use bin scripts of locally installed packages.
So, how gulp can be launched as bin command when installed locally?
What's making it available when locally installed, I reviewed the gulp package.json and the bin scripts, I don't found any answer.
From NPMJS documentation:
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
So, your locally installed packages binaries will be executable like this
./bin/node_modules/.bin/the_binary
This is if you want to launch the binary directly. Or, as specified in the scripts part of the documentation:
In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts.
Thus, you can simply write a wrapper script like
scripts: {
"build": "the_binary"
}
and call your script like this
npm run build
Bonus
As of npm#2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:
npm run test -- --grep="pattern"
You can use lpx https://www.npmjs.com/package/lpx to
run a binary found in the local node_modules/.bin folder
run a binary found in the node_modules/.bin of a workspace root from anywhere in the workspace
lpx does not download any package if the binary is not found locally (ie not like npx)
Example : lpx tsc -b -w will run tsc -b -w with the local typescript package
I'm developing a node package that needs to be run from shell. I know I have to install the package globally, but running
$> npm install -g ./my_module
Does not give me the desired result, that is running
$> my_module
Results in
my_module: : command not found
Instead of running the entry point (index.js) of my node package.
I feel like I'm missing something obvious in here, what am I doing wrong?
After setting up the right package.json configuration, (mainly using {"bin": {...}}), You don't have to publish it to NPM registry then download it again to see it working.
npm link made exactly for this situations. as described in the offical documentation:
npm link in a package folder will create a symlink in the global folder {prefix}/lib/node_modules/ that links to the package where the npm link command was executed.
Assuming you have this project:
-- my_module
-- -- index.js
-- -- cli.js
-- -- package.json
and you have this package.json:
{
"name": "my_module",
"bin": {
"my_module": "cli.js"
},
}
Run:
cd my_module
Then:
npm link
Now npm will install your package globally in your machine. it will check the package.json for the bin entry, and it will link my_module to the cli.js file. This will happen by creating a symlink in the global npm directory to your current directory.
now if you run in your command line:
my_module
it will point to the cli.js file. if you changed cli.js contents, it will be reflected the next time you run my_module, if you renamed my_module to my_module2, use npm unlink then npm link again.
On a different note, npm can use a full url as a package name, it will use the full url to download and install the package instead of looking at the npm registry, you can install packages from your own private Git hosts, for example:
npm install -g https://github.com/Me/my_module
Please try to pack the module and install.
npm pack
and then install it globally
npm i -g my_module-0.0.1.tgz
Let me know is this worked or not
I faced the same issue recently. I developed my module as a CLI with the intent to be able to invoke it from anywhere, published it to the NPM registry and installed it using the -g option but when calling it from the command line I was still getting the command not found error. Adding the bin attribute to the package.json file is what did the trick.
From the NPM documentation:
A lot of packages have one or more executable files that they’d like to install into the PATH. npm makes this pretty easy (in fact, it uses this feature to install the “npm” executable.)
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
Meaning your package.json file should look like this:
{
"name": "foo-cli",
"version": "1.0.0",
"description": "A CLI to do Foo-ish things.",
"bin": {
"foo": "./cli.js"
},
"main": "main.js",
...
}
The property can be a single string if you only wish to specify one single command, or a map if you wish to specify many. Now you should be able to call foo from anywhere in the command line.
How do you create a console app that you can run by a name instead of using node and a .js file to start the app?
Instead of...
$ node myapp.js --out ./folder/
Can I...
$ myapp --out ./folder/
You can use the bin option in your package.json file to make your script executable. From the documentation for package.json:
A lot of packages have one or more executable files that they'd like
to install into the PATH. npm makes this pretty easy (in fact, it uses
this feature to install the "npm" executable.)
To use this, supply a bin field in your package.json which is a map of
command name to local file name. On install, npm will symlink that
file into prefix/bin for global installs, or ./node_modules/.bin/ for
local installs.
Click the link and search for bin: https://www.npmjs.org/doc/json.html for more info.
I have a problem on npm installation
I have created a project say project A
cd ~/projectA
npm install sails
but sails command is not found after installation.
I know that it is successfully install in ~/projectA/node_modules directory.
but the executable cannot be sourced. And i know it is installed ~/projectA/node_modules/.bin
How can I source the .bin automatically whenever I enter into this projectA folder?
Did I did something wrong?
I can give you an inelegant solution, which is working for me. I just exported my path in my .bashrc file.
export PATH="$PATH:./node_modules/.bin"
Edit: It's been a while, but I have changed my strategy to use npm scripts instead. In the above case, setup package.json as follows:
"scripts": {
"sails": "sails"
}
Which you can then run with
npm run sails
or with arguments
npm run sails -- <args>
You should use the npm bin command to get an absolute path to your current node bin directory.
For example:
➤ lessc
bash: lessc: command not found
➤ npm bin
/home/brice/[...]/node_modules/.bin
➤ export PATH=$(npm bin):$PATH
➤ lessc --version
lessc 1.7.3 (Less Compiler) [JavaScript]
This avoids the problem of relative paths, especially if you're going to be using this in a build system that will invoke the command in subdirectories.
A bit more robust is:
export PATH=$(npm bin):$PATH
You can either run it, add it to your shell profile, or create an alias like:
alias snpm='export PATH=$(npm bin):$PATH'
If you do go the alias route, be sure to use single quotes so it delays the execution of the variables!
To use on the command line like sails generate foo you will need to install the npm module globally.
npm install -g sails
You could also use the path to the bin in the command if you don't want to install globally:
./node_modules/sails/bin/sails.js generate foo
The official instructions for sails (https://github.com/balderdashy/sails) advises
To install the latest stable release with the command-line tool:
sudo npm -g install sails
This installs globally and adds to a directory like /usr/local/bin that should be in your $PATH.
But to answer the general question regarding the location of the binaries if you install locally, they should be placed in ./node_modules/.bin directory (so run ./node_modules/.bin/sails ...)
If you don't like to mess up with your PATH for running a npm script that isn't global -- e.g. you are the only one to use it --, I would personally recommend the use of an sh "alias".
npm install (locally) your beloved package (json-diff here, for instance)
cd ~ && npm install json-diff
alias it (save it in your ~/.xxxxrc file):
alias diffj "\`npm bin\`/json-diff !*"
Then, for diffing 2 json's:
diffj old.json new.json
In my ~/.bashrc, I have the following:
function nbin {
local dir;
dir=$(npm bin)
if [ -d "$dir" ]; then
( # subshell to not change this shell's $PATH
PATH=$dir:$PATH
eval "$#"
)
else
echo "\"$dir\" is not an npm binary directory." >&1
return 1
fi
}
I can then run executable foo in the .bin directory as:
nbin foo
Possible workaround with NPM 5.2+ using the npx command.
npx sails new test-project
See this question for a similar use case and elegant solutions.