npm package installation via Package vs Command Line - node.js

What is the difference between these two installation methods for some npm packages?
This excerpt is from the installation section for pug:
Package
via npm:
$ npm install pug
Command Line
After installing the latest version of Node.js, install with:
$ npm install pug-cli -g
So these are my questions:
What are the advantages of using either of these two methods?
Why are these packages named differently (pug vs pug-cli)? I've noticed that some npm packages are suffixed with "-cli", command line interface.

npm install pug: This is basically used to install the node module pug within the dir you run this command from. Once installed it lets you use pug with the project.
npm install pug-cli -g: This is installing the pug's command line interface. The -g flag installs it globally which means, you could basically initiate pug-cli from anywhere.
Now, the CLI's in general provides a lot of methods on top of the underlying modules. These methods are not something you would need in your code, but more as convenience methods, like say starting a server, or for the case of pug, the CLI provides a methods to render all the templates in a particular directory.
As the part of your project: You would need Pug to write those those templates and convert them to HTML. But say you want to test a few templates, then you could use Pug-cli for that, instead of building up your code and then looking in the HTML's.

npm install pug: This is basically used in your current working project, it tells your node.js to render pug.
npm install pug-cli -g: using pug-cli gives you the power to be able to run your pug from the terminal, and including the pug-cli -g implies that your pug-cli will be installed globally which means you can run it from any CWD in your terminal.
Therefore all -cli (command line interface) modules have that ability of being ran directly from the terminal.

Related

What is the difference between installing a package locally and globally using npm?

What is the difference between installing a package locally and globally using npm?
From my understanding:
Locally install: npm install <package>
This package/module will find on your local node_modules folder and
can only be usable for this project.
This package/module can be accessible in using require("package")
from code.
This package/module can't be accessible in command line interface.
Globally install: npm install <package> -g
This package/module will find on where node is installed in your machine like /usr/local and can be usable everywhere.
This package/module can't be accessible in using require("package")
from code.
This package/module can be accessible in command line interface.
Please let me know. If I could misunderstand anything here. Thanks!
You are correct except for 1 point.
The local packages exposing CLI utilities can be accessed from the command line. Newer versions of NPM create this .bin/ directory inside the local node_modules/.
Whenever you try to use a tool (let's take babel for example), if you use it from the command line and you have it installed in your project, npm will properly identify that package and run it's CLI for you.
Here's a useful article on the topic.
http://www.2ality.com/2016/01/locally-installed-npm-executables.html
Global modules are mostly tools like gulp, yoman or any other module you use in your daily work.
Local modules are the dependencies of your project. You should never depend on a global module in your project. Even dependencies as gulp should be a local dependency in your dev-dependency section.

unable to run npm command. tty.js not installing

I am working on an application that extends Adobe brackets. Here I need to add terminal/console in that. I have installed node.js and npm server already.
Now i need to run npm install -g tty.js command but it show me this error
work-round->
try without -g, this will #least install on local dir. more over u need 2 come in terms with npm support team.
email support#npmjs.com to get your copy of this package..
you can also work with stand-alone copies available on i-net.. here is one such link where you can find full source-code for term.js..
https://github.com/chjj/term.js/blob/master/src/term.js
OR
install web-terminal package from npm, with CLI use command : 'sudo npm install web-terminal -g'

What does the -g option for npm install and npm list do?

The Node.js npm (Node Package Manager) has a -g command line argument, which I often see referenced. For example, the documentation for the Microsoft Azure x-plat (cross-platform) CLI tool says to install it by using npm install -g azure-cli.
Question: What does the -g option do?
What options do I have to install node modules?
After writing this I quickly found and old but still applicable post by Isaac (yes, the npm #isaacs). But I still think the below post is informational.
You can install npm modules globally or locally - you already know that, but why?
Globally: npm install -g some-module-a: This module is intended to be used as an executable (i.e. CLI, file watcher, code minifier, logger, etc.).
Locally: npm install some-module-b: To be imported and used in your app via import, var someModule = require('some-module)
global modules are one of the best ideas of npm. We can easily create executables using node/javascript. If your node app is meant to be run as an executable, then you will want others to install it globally. If it's a utility, helper, application, etc. then you usually don't want it installed globally. So, unless the module explicitly states that you should install it with -g, then don't.
One more time: if you are wanting to use some module called some-module in your node app - var someModule = require('some-module'), then npm install some-module from the root of your node app to pull it into your local node_modules directory. If you've installed some-module globally and not locally, it will usually not load and will show you an error about not finding the module (even though it can be made to load the global module - hint: just don't!)
So what exactly happens when you install globally?
npm install -g [some module] installs the specified node module in a directory higher up in your file system (i.e. usually /usr/local/lib/node_modules in unix systems). The biggest use case for global modules is for CLIs written using node (think npm, bower, gulp, grunt, et. al.).
Let's look at what happens when you install bower globally:
*follow these steps in your command line/terminal
step: npm install -g bower
explanation: the module - all of it's files and dependencies - are saved in your global directory (e.g. /usr/local/lib/node_modules/bower).
Something else happened here. Somehow you can now run bower in your command line. Awesome!
step: bower -v --> results in the installed bower version (i.e. 1.6.5)
explanation: It's now a fully executable node app using bower as the keyword. Inside bower's package.json file you'll find a bin property:
"bin": {
"bower": "bin/bower"
}
So how did that all work?
npm will create a symlink from where most executables live, /usr/local/bin/bower over to /usr/local/lib/node_modules/bower/bin/bower, where the module lives. That symlink makes it so when the executable runs, it can reference other files in the original module, including it's local node_modules. Pretty cool, huh?
*Note on executables: If you create a file called awesomeness in /usr/local/bin/ and chmod u+x (user + executable) it. Then write some scripting in it (in this case javascript using #!/usr/bin/env node at the top). Then you can run it anywhere in your command line/terminal just by typing awesomeness.
Hope that helped. I know doing a deeper dive into it helped me early on.
Node.js packages can be installed one of two ways:
Globally
Locally
The -g option instructs npm to install the package globally. You would install a Node.js package globally, if you want to be able to call the command directly from the terminal.
From the documentation:
There are two ways to install npm packages: locally or globally. You choose which kind of installation to use based on how you want to use the package.
If you want to use it as a command line tool, something like the grunt CLI, then you can want to install it globally. On the other hand, if you want to depend on the package from your own module using something like Node's require, then you want to install locally.
To download packages globally, you simply use the command npm install -g , e.g.:

Path of the express

Why express command not found. Here is the path for express.
/usr/lib/node_modules/express
When I install the express, the terminal shows the path. I used
npm install -g express-generator
npm install -g express
But when I run express, it doesn't work. In this directory, express is globally right? But why can't be found. I don't understand the logic.
You need to install express locally, rather than globally.
http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation
In general, the rule of thumb is:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
Based on this, you want to install express-generator using the -g flag as you will use it as a command line tool, but you want to install express without this flag as it's a module you will want to require() it in your application.
Take a look at the installation guide:
http://expressjs.com/starter/generator.html
It says to install the express-generator module as a global module as you will use it as a command line utility to scaffold your new applications.
Once you have scaffolded an application using the express myapp command, you just have to run npm install in the myapp directory which will download the rest of the dependencies to your projects local ./node_modules directory. It does this by reading the contents of the generated package.json file.
Lesson to take away: Don't install with the -g flag unless the modules instruction guide explicitly says to do so.

How to edit global file installation

I just ran this line of code
npm install -g csslint
CSSlint seems great but I want to add the ability to prettify my css by putting in custom code. The goal is to have a command line interface where I can format my css before pushing the code to the server.
"-g" stand for installing globally correct?
Where does this library get installed to and how do I access it? I thought it would be in my user/local/bin but I couldn't find it.
Thanks!
use npm root -g to see where modules get installed. If you want to use it in your code using require() then install it locally i.e. without using -g option. More details here https://npmjs.org/doc/faq.html
I believe it's going to depend on your npm settings, but my global npm binaries are in /usr/local/share/npm/bin

Resources