How to use node modules from command line? - node.js

I can't find any straightforward instructions on this. I've installed node.js and npm, then created a project in its own folder, D:\node_stuff, then cd'd there via cmd (Windows 10) and ran npm install express, npm init. I'm trying to use gifify, and installed its dependencies via npm instead of brew (ffmpeg, imagemagick, giflossy).
gifify -h -> 'gifify' is not recognized as an internal or external command,
operable program or batch file.
node gifify -h -> Error: Cannot find module 'D:\node_stuff\gifify'
cd node_modules -> node gifify -h -> nothing happens
What am I doing wrong? Where do I even look - all tutorials with simple search only show how to install packages or build a project - I don't need to build anything, only to use this one module.

You can run npx gifify -h.
Generally, you have two options when installing NPM packages:
install globally e.g. npm install gifify -g
install locally e.g. npm install gifify (or npm install if the package is listed in package.json)
Some packages, when installed, also install a command-line script. For globally-installed packages, that CLI script is installed to a location that is in your PATH and hence you can simply run the bare command e.g. gifify -h. For locally-installed packages, that CLI script is installed locally under the node_modules folder, which is not in your PATH. To run such a script you can use the NPM package executor npx, for example npx gifify -h. This essentially executes the local script from the node_modules/.bin folder.
If your package script, e.g. gifify, relies on third-party executables such as FFMPEG and ImageMagick, then I would install those as regular applications (which will put them on your PATH).

Related

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

how to set grunt variable env path

I am trying to set grunt for my node.js project and I have followed below steps:
1)I have installed node.js and it is working fine.
2)Installed git.
3)Installed grun by running: npm install -g grunt-cli.
C:\Users\user\Downloads\bpost-gs-api-1.1.1>npm install -g grunt-cli
C:\Users\user\AppData\Roaming\npm\grunt -> C:\Users\891153\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
C:\Users\user\AppData\Roaming\npm
`-- grunt-cli#1.2.0
Now I have run the cmd grunt -version, I got below error:
grunt is not recognized as an internal or external command,
operable program or batch file.
Can you please help me to set up grunt for my project. How to setup variable env and how to setup path etc.
Appreciate your help
https://gruntjs.com/getting-started
In order to get started, you'll want to install Grunt's command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this.
npm install -g grunt-cli
This will put the grunt command in your system path, allowing it to be run from any directory.
Note that installing grunt-cli does not install the Grunt task runner! The job of the Grunt CLI is simple: run the version of Grunt which has been installed next to a Gruntfile. This allows multiple versions of Grunt to be installed on the same machine simultaneously.
start from reading this:
https://gruntjs.com/installing-grunt
If you need a specific version of Grunt or a Grunt plugin, run npm install grunt#VERSION --save-dev where VERSION is the version you need.
for better understood on how to start setup a grunt project: https://gruntjs.com/getting-started#preparing-a-new-grunt-project
A typical setup will involve adding two files to your project: package.json and the Gruntfile.
package.json: This file is used by npm to store metadata for projects published as npm modules. You will list grunt and the Grunt plugins your project needs as devDependencies in this file.
Gruntfile: This file is named Gruntfile.js or Gruntfile.coffee and is used to configure or define tasks and load Grunt plugins. When this documentation mentions a Gruntfile it is talking about a file, which is either a Gruntfile.js or a Gruntfile.coffee.

Why can I run globally installed node modules by name?

I install a node module globally, let's say the grunt module. I install it by:
npm install -g grunt
It's installed in %APPDATA%\npm\node_modules\grunt.
Then I can run it in command line, like grunt --version. How does this happen? I mean, why can I directly use grunt as a command?
BTW, I'm using Windows. And I install NodeJS by .msi installer.
You aren't really running the grunt package as a whole from the command.
The setup for this starts in grunt's package.json. In that, it's specified a bin script that's named the same as the package.
"bin": {
"grunt": "bin/grunt"
},
When you install the package globally, npm adds an executable file for each bin script (there can be multiple per package) to a directory in your system's PATH, allowing a command line to find them when you type the command.
When you run grunt, it's sort of a shortcut to running node bin/grunt from the directory where it's installed, passing along any arguments you provided after it.

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

node npm local install puts files into ~/node_modules

When I install a package using npm install command, it installs the files into ~/node_modules. When I run the package, I get command not found error.
How do I install it into a folder where I want to call the package?
npm install <name_of_package> -g
This will install the package globally. If the program is in your PATH, then you should be able to run it just like any other program.
For example:
npm install nodemon -g
then run nodemon from the command prompt, and it should work
If you don't want to install it globally, the right answer is the last comment in the checked answer:
Simply add ./node_modules/.bin to your PATH, and all the commands installed locally by npm will be available. – H_I Dec 24 '12 at 9:54
You can add it to your path in your .bashrc file using the command:
export PATH="$PATH:/home/login/node_modules/.bin"
Reload your .bashrc using:
source .bashrc

Resources