Why executable is found - node.js

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

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.

NodeJS - npm install practice

Created new folder and did npm install serve in it.
It created package-lock.json and node_modules/ folder.
When I run in the same folder serve it shows error:
command not found: serve
What is the way to install?
I am using: npm#6.5.0
My dev environment is MACOS
I read a great many pages on this topic and nothing worked until I tried the following
./node_modules/.bin/serve -s build
Also if you are using VS CODE you may want to bring up the terminal window outside of VS CODE - this seems to have snared a lot of people.
First of all, you should start your project running
npm init
This will create the package.json file.
Then, you can install the serve package globally.
npm install -g serve
And now you can run serve.
The serve binary was not found because the operating system cannot locate it in the PATH environment variable.
When you do the npm install serve command. The serve module is only installed into the node_modules directory found under the the project folder. Unless you explicitly include the absolute path of this node_module directory as part of your PATH env var, the OS won't know where to find serve.
Like others say, the typical practise would be to install the module using the -g flag. G means global.
When -g is used, npm will put the binary in its node directory somewhere and this this directory would have been included as part of your PATH when you install node, thus making the any new binary discoverable.
If the node.js module has a "command" and you want to run it without installing the module globally(npm install -g serve). You can run it like ./node-modules/.bin/command from the root folder of the project.
Now, what is generally used is npx, so that you can from within a project easily run any of the binaries within its local node_modules or your system's global node_modules/ and any other commands on the $PATH.
For example, here we install webpack as a local dependency. You can image doing this in a folder after running npm init. Then we run webpack without having to worry about referencing the bin file:
$ npm i -D webpack
$ npx webpack

Run npm command line without add node to path

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

How do you run gulp if gulp is installed(node_modules) in different folder than gulpfile.js

I have gulpfile.js in one directory and node_modules in another.
When I run gulp, i get the error -
'Local gulp not found in '..(the directory)..
Try running: npm install gulp'
The thing is - I cannot install gulp in the directory of gulpfile.js and so I need a way to tell the gulp to refere to the other directory i have gulp installed in.
You don't need to install gulp globally if you don't want to. What you can do is run your gulp executable (from your node_modules) and then pass in the location of your gulpfile using the --gulpfile parameter. Also, if you want to control where your gulp is running, make use of the --cwd parameter.
Here's an example:
<NODE_MODULES DIR>/gulp/bin/gulp.js --gulpfile <GULP FILE> --cwd <SOME DIR>
There is no need to install gulp globally.
First install gulp (ideally on dev dependencies)
npm install gulp --save-dev
Then in the package.json add the line you want to run
"scripts" : {
"gulp" : "gulp"}
}
Finally in the command line use
npm run gulp
npm will use the binary from the node modules without any need to install it globally or to write down the whole path
You need to install gulp globally:
npm install -g gulp
This will allow you to run gulp from the command line in any directory.
Put the node_modules folder in parent directory always, then make project directory as child/grandchild folder.
Don't put the node_modules folder in any child directory
Folder structure will be like:
parent
└──node_modules
└─project_1
└─project_2
In any child/grand child directory gulp will work

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