run global installed npm programs with npm - node.js

So you have npm bin -g that yields the bin directory where npm installs stuff. Is there a way to use npm to run stuff from that directory? An obvious option would be $(npm bin -g)/installednodeprogram but I am calling this from an external program (emacs) and would like to avoid bringing up a shell. Also I could get the path from npm bin -g and then concatenate the program name and do all sorts of checks but I hope there is a cleaner way I couldn't find like npm run-binary installednodeprogram.

If you've installed a program globally, you can run its command from the shell just like any other. In fact, that's what the npm command itself is.
For example:
npm install -g browserify
browserify file.js # browserify command is now available

Related

what's the destination does 'npm install -g xxx' install to?

For instance, npm install -g sinopia
On windows7, I will install the sinopia command and related modules inside C:\Users\xxxx\AppData\Roaming\npm.
On Redhat5, my node and npm command are in /usr/local/clo/ven/node-v4.2.3-linux-x64/bin. When I run 'npm install -g sinopia', by default, sinopia was got installed in the current directory as npm and node like below.
But currently I got a linux machine that has got sinopia installed by other person. I can not find the start script of sinopia inside node/bin, and I can find sinopia related stuff like below.
.
Where can I find the start script of sinopia? Whether the installation location of the 'npm install -g xxx' can be configured?
npm installs packages globally to its set global "root". To find what the global root is in a given environment, run npm root -g.
It will typically be inside the "prefix" directory, which you can find with npm prefix -g.
Note you can also change the prefix directory with npm config -g set prefix </new/prefix/path>.
To answer your more specific question
In order to find the sinopia executable, you can run which sinopia on linux (you might need to install which on RedHat, it should be available in your package sources). It will give you the pathname of the file that would be executed for the sinopia command.
But that could be a symlink to another location; to resolve the pathname you can use readlink -f $(which sinopia) on bash. The -f option tells readlink to follow links recursively. $(which sinopia) will be substituted by the output of the which sinopia command.
Quick edit as I see this has already been answered above.
To find the install location of packages installed globally through npm, run the following:
npm config get prefix
to update this you can use the following command:
npm config set prefix path
Source: npm global path prefix

unable use any installed npm packages

I'm trying to use node on my fedora 21 installation. However whenever I install a package via npm I am not able to to call that package such as gulp.
npm install -g gulp
then : gulp run
error:
zsh: command not found: gulp
You need to point your global $PATH variable to the location of node. Otherwise, it will return that error.
Open your .zshrc file and ensure you see something like PATH=/usr/bin/node:$PATH (ensuring your directory is the proper one for node). You can find that directory location with the command npm -g bin.
If that doesn't work, comment below. Thanks
The directory where npm installed gulp is not in your path. To find out where it was installed, run npm -g bin and make sure the directory is in your path. Alternatively, execute gulp as $(npm -g bin)/gulp.

How to put local node package on path?

Newbie question. I have chosen not to install express with -g option. I did not use npm -g which would put it on the path globally. Instead it is installed in my local mac user directory. What I am not clear on is exactly what or how you put a package like express on the path so it can be invoked etc? What exactly needs to be on the path (node_modules?) so these packages are available just like a -g installation? I could have used home-brew I suppose but anyway, I now have all node packages and everything local. Another situation is that I am not able to run any of the nodejs tutorials. Although there might be smarter ways to do this, I wonder if sudo is really such a good way to install a development package ....
Now for example, I want to run the tutorial javascripting which is a nodejs tutorial. How do I do this. If I just type:
Mac1$ javascripting
it finds nothing.
Same for
Mac1$ express
UPDATE: THIS WAS ANSWERED IN THE COMMENTS
The commands exist in a hidden directory after a regular
install npm install express
in my case this the command goes here: /users/MAC1/node_modules/.bin
It is this path that needs to be placed on the $PATH as described in the first comment.
Thanks guys.
npm installes executable to two places. By default running a npm install in a project will install any binaries in ./node_modules/.bin. When you use the -g flag (npm install -g package-name) it will install into a global path. You can find out the global path by running npm bin -g. Add that to your path and globally installed executables will be accessible.
You can also add ./node_modules/.bin to your path to allow easy access to executables added by packages in your project folder. I admit to using this on a trusted local machine. However, this is very dangerous and not a recommended way to expose the executables in the node_modules directory.
Best alternative is to add the executable to the scripts section of the package.json file and then use npm run-script <command> which will auto prepend the ./node_modules/.bin when executing.
package.json
{
"scripts": {
"foo": "foo --arguments"
}
}
Example
$ npm install foo
$ ls ./node_modules/.bin
foo
$ npm run-script foo
# Executes:
./node_modules/.bin/foo --arguments

Making "sudo npm install --save-dev gulp" in Maverics nothing's happens

I'm trying to test Gulp and I find this problem.
I installed Gulp globally with: sudo npm install -g gulp.
Then I was created the folder: mkdir my-gulp
cd my-gulp
And the I try to do this: sudo npm install --save-dev gulp
It seems work fine, no errors, but my folder my-gulp continues empty.
I can see in console, this line:
gulp#3.8.6 ../../node_modules/gulp
And I thinkig the problem is here "../../", I think it must be "gulp#3.8.6 node_modules/gulp"
What do you think? Maybe is something about $PATH?
First, you should never run npm via sudo for local packages. (In fact, you shouldn't run it as root ever, but that's a different, more complex issue to solve.)
Second, you need to initialize NPM before you can install packages. It sounds like you didn't do this.
There's two ways:
If you have an existing package.json, which contains the required information about a project, you can copy it into your folder, then edit in a text editor of your choice to get started. If the package.json includes existing dependencies, you can either remove them, or run npm i in the folder to install them.
If you are starting fresh, run npm init to set up a new package.
Once you have a valid package.json, you can install packages into your application.
As to why you are seeing ../../gulp, I'm guessing you have a package.json further up the file tree somewhere.

Possible to include a command-line in a Node module without global install?

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.
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 ...

Resources