8

I have a small Node module that includes a command line script in the bin directory.

 "bin": { "generate": "./bin/generate.js" } 

The generate.js script is properly executable.

This all works fine if I run npm install -g. But I'd prefer not to globally install and only have the command generate work from inside the module folder. If I run npm install from the module folder, it does correctly install all of the dependencies in a node_modules subdirectory. But then generate from the command like gives me "No such file or directory."

Thx.

1 Answer 1

12

I never install node modules using -g. My solution for your problem is to add this to my $PATH

# add this to ~/.bashrc, ~/.zshrc, or ~/.profile, etc export PATH="./node_modules/.bin:$PATH" 

Now, so long as your in the root of your module, you can access any binaries that have been installed as modules.


As an example, less is commonly installed with

npm install -g less 

However, if you have your PATH modified as described above, you could something like this

cd my_node_module npm install --save less lessc less/style.less css/style.css 

Without the PATH modification, you would've seen

command not found: lessc 

If you don't feel like altering your PATH, you can access the binary directly

cd my_node_module npm install --save lessc ./node_modules/.bin/lessc a.less a.css 

Yay, no more npm install -g ...

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, perfect response. I think you mean npm install less. (Lessc is name of cli)
An executable is created everytime whether it is globally or locally installed. In latter case, you just need to modify the PATH.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.