Run npm command line without add node to path - node.js

I have a folder with last version of node and npm (on ubuntu) :
node
node_modules
node
npm
npm.cmd
And would like to run node/npm without add node/node in classpath
Because node/npm returns :
node/npm: node: not found
Idea ?

What you can do is to call the npm-cli.js using node. So in the node folder, you can run npm commands by running:
./node node_modules/npm/bin/npm-cli.js <command> <args>
where and are the npm command and arguments.

This is impossible.
Any executable that you run by writing its name (not including the path) is looked in some pre-defined paths.
If you'd like to run an executable located in some directory, you can execute it only by specifying its full or relative paths.
If you change dir to the containing directory, you can run it by
./executable-name
In your case
./node

Related

How does NPM resolve & run scripts from package-json?

For curiosity's sake, I want to create a folder in node_modules and be able to run it as a script in package.json's "scripts", but I have not been able to find out how to.
I've noticed with other package.json scripts like "react-scripts" that they wont work in the command line, but will when NPM runs them. How does npm resolve scripts?
Does it have to be a legit package for NPM to run it? (Like, will it check against the NPM registry for if that package exists, or contains scripts?)
If a module has a ["bin" entry in its package.json]1, then npm will put the executable script specified by that "bin" entry into a .bin folder in node_modules.
When running a script with npm run, npm will put that node_modules/.bin directory first in the PATH so they are found with npm run but not when run directly from the shell.

Add path to node in package.json

I use Plesk and CentOS. My npm and node bin are in a specific folder :
/opt/plesk/node/9/bin
When I try to run /opt/plesk/node/9/bin/npm install, I have this error :
node-sass#4.10.0 install /var/www/app/node_modules/node-sass
node scripts/install.js
sh: node: command not found
I guess it's because Plesk doesn't set PATH for node, I have multiple versions of Node.
Can I set the path to node in the package.json of my project ? or set this path in an other way ?
Because Node binary was not found by npm due to no node binary in the current PATH.
Try add the variable into a global npm config file. Using shell command:
echo "scripts-prepend-node-path=true" >> /opt/plesk/node/9/etc/.npmrc
Or you can use File Manager of PleskUI to add scripts-prepend-node-path=true into .npmrc file. Create the file if it does not exist.

Why executable is found

I do npm install grunt -g and the folder is created C:\Users\myname\AppData\Roaming\npm\node_modules\grunt-cli. In my path variable only the C:\Users\myname\AppData\Roaming\npm part is added. How then when I write grunt in the command line the grunt executable inside \grunt-cli folder is found?
That's because when you install grunt-cli, npm creates a file grunt (and grunt.cmd) inside C:\Users\myname\AppData\Roaming\npm so directly in your the folder in your path
If you look into this file you can see it calls node_modules\grunt-cli\bin\grunt

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.

Modules are not installing globally with "npm -g install..."

On Ubuntu 12.04 x 64...
npm -g install hiredis redis
Installs fine and npm ls shows those modules, but only when I'm in node source directory
does not show when I'm in any other directory
For kicks, tried running the command while in that other directory- still no dice :(
They are installing globally, but you cannot see them with npm ls, in other directories. Because npm ls only shows local modules. If you want to list global modules you have to type: npm ls -g.
Sometimes another version or just a wrong path is referenced in the npm config file instead of the installed version.
This may cause node/npm to misplace global modules.
To check and fix:
In cmd line type: npm config list
You should get a list of configuration values, one of them is prefix.
Make sure the path in prefix is the same path (only without node.exe) as the actually installed node.exe path.
(this path is listed further down as node bin location)
If it's not, change it:
Either in the config file (in your user folder, named .npmrc)
Or, via cmd line: npm config set prefix "C:\Program Files\nodejs" (change path for ubuntu of course)
Reinstall the module/package you tried to install, don't forget -g for global.

Resources