Node NPM - install versus install -g - javascript

I am a node newbie and am somewhat confused with the whole "install" thing.
What is the difference between install, and install -g?
Can something installed with install -g be accessed anywhere, or does this make it available to the Node server but not your application? Is there any reason to use one, and not the other?
Cheers

From the node.js blog:
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.
So for example, lets say you wanted to install the Grunt CLI. Odds are you'll use Grunt in multiple projects, so you'll want to install that globally using -g.
Now lets say you're working on a project and your project is going to require a module such as Express. You would cd to your projects root directory and install the module without -g.
Here is a more in depth explanation.

install means the module will be created in your local node_modules folder which is highly recommended for anything your application relies on (for versioning, amongst other reasons).
install -g means install the module globally on your machine. This is generally only recommended to use with modules that perform some task unrelated to the execution of your application.
Simple examples of this are Yeoman generators, the Express generator, PhantomJS, etc.
There is an official blog post about it here

The only difference is npm install mod will install it in your local directory. Let's say you are working in 'projectA' directory. So
> npm install mod
will install "mod" in
> projectA/node_modules/mod/
so any .js file inside projectA can use this module by just saying require('mod')
whereas 'npm install mod -g` will install it globally in the user's node module directory. It will be somewhere in
> /usr/bin/npm/node_modules/modA
you can use this module anywhere in any of your project, in addition to that if there is any terminal command is there in 'modA'. It will be accessible from you terminal directory.
> modA --version
> version 1.1

Related

Install npm packages locally to be accesed by other projects

I'd like to know how it works npm comparing to Maven (I come from a Java background) in terms of managing packages.
I have created a generic component using Angular 4, this component will be used in many projects. So, I have published it to our nexus registry and then, from the client projects I just import the package so that the package gets downloaded from the registry and everything works fine.
Now, for development, I don't want to be publishing to the registry every single time I do a modification in the generic component and rebuilding the clients.
I would like instead to do it like we do with Maven in Java, we install the artifact in our local repo, and the artifact will be picked up from the local repo before going to the global 'artifactory', but I see that when we install a module using npm, it gets installed inside node_modules folder inside the same project, so that the module is not available for any other project.
How should I do that? In other words, does npm keep a local repository where the installed modules are accessible to any other projects without the need of publishing to the global registry?
Thanks
use --global switch behind the npm install command to install the package of your choice global.
hope that helps
To make something available to the rest of the system's node package environment through npm, you can install it globally (which is local to your system) rather than locally (which is local to your project). You can see documentation for global-ness on installs in this part of the NPM documentation.
npm i -g package names here
npm install --global package names here
You can update your globally installed packages as you would a locally installed one as well when you need to.
npm update -g package names here
(or all of them without specifying)
npm update -g
See the full NPM documentation pages for more detailed flags, etc.
If you're hoping to use your own packages in a managed environment, you can either publish them as private modules or keep them in a VCS (mostly git) and reference them by the appropriate method for that VCS in your projects' package.json scheme through the dependencies block for github urls or more generally other git hosts, like
"dependencies": {
"myComponent": "user/repo#feature\/branch",
"otherComponent": "git+https://myGitHost.tld/.../projectName.git#commit"
}

How to install Node.js or npm on Xampp

I'm new, so please excuse any mistakes.
I am trying to develop a web application locally using XAMPP. I intend to deploy using a hosted web service. XAMPP was a very convenient way to get me up and programming without any fuss.
There are several github javascript libraries that look like they can only be installed with npm. Does this mean that I cannot use them? The specific one that I'm currently having a problem with is: https://github.com/selz/plyr. Or, is it possible to install npm on my XAMPP server? I have a windows machine.
Thank you very much in advance for your help.
is it possible to install npm on my XAMPP server
You will not be installing NPM on your server, you will be installing it on your machine (think of it as a regular program you install on your PC) (if your server happens to be your PC as well, then, well, you are technically installing it on your server)
In order to be able to use NPM though, since NPM is written in JavaScript, you will need to install Node.js, also on your machine.
So, head over to the Node.js site and download and install Node. NPM is included in the installer and will be installed alongside Node.
After that is done, installing NPM packages is simply a matter of heading to your desired directory and executing npm install <PACKAGE_NAME> there.
In general, XAMPP helps you with your back end. Not your front end.
Useful links:
https://docs.npmjs.com/
Does this mean that I cannot use them?
No. You can install node at https://nodejs.org and then use npm through your windows command line to install packages. To test this, just
Install NodeJS
Open Command Line
Make a new folder under your user folder called test mkdir test
cd into that folder on Windows Command Line cd test
Create a new project with npm init -y *note this creates a
package.json file for you
Now you start installing packages with npm install <package>
--save * --save saves the package info in your package.json file (recommended). There is also -g wihch means the package will
be installed globally (usually you don't want this and only want it
locally to your project).
That's how you use NodeJS. If you want to use that with XAMPP then you need to do the same thing but instead of using your user folder (C:\users\yourname) you need to use the htdocs folder of XAMPP (usually C:\xampp\htdocs)

What's the js equivalent of RailsInstaller?

I want to build a small js plugin, and I want to try that with ReactJS.
ReactJS recommends installing using npm and browserify.
In my experience with Ruby on Rails, there are always a lot of things to install, and using Windows introduces additional problems.
With Ruby it is Rails Installer. What is JS equivalent of Rails Installer? i.e. a tool that lets me install all necessary packages with one step?
I did a little bit of search, find that I need to install:
NodeJS, NPM
NVM
Webpack or Browerify
Babel
I think there may be others that I need.
To start the project you need node and npm because webpack, browserify and babel is npm packages.
The only way to install all packages in one step is to install package that actually depend on all packages you need. There is a lot of ReactJS starter kits in github like this https://github.com/kriasoft/react-starter-kit
I, personally recommend you doing such a things by yourself - its not a big deal if you understand what you want. In the root of all nodejs projects there is a file package.json every time you need a new module (package) just type npm install -S <module_name> in the root directory of your project.
This new module appears in node_modules directory and because of -S flag it name also will be stored in dependencies of your project in package.json file so that in the future you can just type npm install to install all dependencies (aka modules/packages) of your project.

How to verify an object instance? instanceof and ....prototype.isPrototypeOf(...) are not reliable [duplicate]

Whenever I make projects, I have to download all dependencies of node modules. Without copying the node_modules, Is there anyway to share the central node_modules in multiple projects?
like the followings, I have to run many commands every time..
npm install gulp-usemin
npm install gulp-wrap
npm install gulp-connect
npm install gulp-watch
npm install gulp-minify-css
npm install gulp-uglify
npm install gulp-concat
npm install gulp-less
npm install gulp-rename
npm install gulp-minify-html
You absolutely can share a node_modules directory amongst projects.
From node's documentation:
If the module identifier passed to require() is not a native module,
and does not begin with '/', '../', or './', then node starts at the
parent directory of the current module, and adds /node_modules, and
attempts to load the module from that location.
If it is not found there, then it moves to the parent directory, and
so on, until the root of the file system is reached.
For example, if the file at '/home/ry/projects/foo.js' called
require('bar.js'), then node would look in the following locations, in
this order:
/home/ry/projects/node_modules/bar.js /home/ry/node_modules/bar.js
/home/node_modules/bar.js /node_modules/bar.js
So just put a node_modules folder inside your projects directory and put in whatever modules you want. Just require them like normal. When node doesn't find a node_modules directory in your project folder, it will check the parent folder automatically. So make your directory structure like this:
-myProjects
--node_modules
--myproject1
---sub-project
--myproject2
So like this, even your sub-project's dependencies can draw on your main node_modules repository.
One drawback to doing it this way is you will have to build out your package.json file manually (unless someone knows a way to automate this with grunt or something). When you install your packages and add the --save arg to an npm install command it automatically appends it to the dependencies section or your package.json, which is convenient.
Try pnpm instead of npm.
pnpm uses hard links and symlinks to save one version of a module only ever once on a disk.
If you have npm installed, you can install in your terminal with:
npm install -g pnpm
To update your existing installations (and sub-directories) use:
pnpm recursive install
Or use the shorthand command (leave off -r if you need to target only one directory)
pnpm -r i
One helpful note: You may find some rare packages don't have all their dependencies defined. They might rely on the flat node_modules file directory structure of npm or yarn installs. If you run into issues of missing dependencies, use this command to hoist all the sub dependencies into a flat-file structure:
pnpm install --shamefully-hoist
It's best to avoid using the --shamefully-hoist flag as it defeats the purpose of using pnpm in the first place, so try using the command pnpm i your-missing-package first (See pnpm FAQ).
I found a trick, just take a look at the Symbolic Links (symlinks) on Windows or Linux, it is working just like shortcuts but more powerful.
Simply you need to make a Junction for your node_modules folder anywhere you want. The junction is nothing but a short cut to your original node_modules folder. Create it inside your project folder where the actual node_modules would have been created if used npm install.
To achieve this you need at least one node_modules real folder then make a Junction to it in the other projects.
On Windows, you can either use the Command Prompt, or use an application. Using the Command Prompt gives you a bit more control, using an application is easier I suggest Link Shell Extension.
Main directory should look like this
node_modules
Project 1
Project 2
Project 3
Project 4
just open the file Project 1/.angular-cli.json
change the schema
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
to
"$schema": "./../node_modules/#angular/cli/lib/config/schema.json"
and don't forget to create node_modules empty folder inside your project directory
See also npm v7.0.0's support for workspaces
RFC
https://github.com/npm/rfcs/blob/latest/implemented/0026-workspaces.md
Documentation
https://docs.npmjs.com/cli/v7/using-npm/workspaces
By looking at some articles it seems that Lerna
is a good tool for managing multiple projects inside a single directory (monorepo). It supports modules sharing without duplicating the entire packages in every folder and commands to install them in multiple projects.
Javascript monorepos
Monorepos by example
Building large scale apps in a monorepo
pnpm is also a simple and efficient tool, which doesn't duplicate those modules which are already installed for other projects.
Let's assume that having a single node_modules it should contain all the packages for all applications. thus your apps will also share most of the unique package.json entries (just the name should change)
my idea would be to have a single root and multiple src level as below
root\package.json
root\node_modules
root\\..
root\app1\src\\..
root\app2\src\\..
the only issue you might face would be having a backup of json (or tsconfig) for any app and restore them when you work on it or setup your startup scripts to serve any app

Trouble installing a module as a global variable -- /usr/bin/env not a directory

I am attempting to install the node.js module 'javascripting' (source code can be found: https://github.com/sethvincent/javascripting) and have been unable to install it as a global variable to run through the terminal.
After installing node.js I attempted to install javascripting with the line: npm install --global javascripting
While it is my understand that this should work, it only downloads the module but does not set it as a global variable to be run in terminal.
The error I receive when attempting to run it as a global variable is "/usr/bin/env: node: No such file or directory".
After receiving this error I attempted to move the module to /usr/bin/env from the directory it installed in (usr/local/lib/node_modules/javascripting). Unfortunately, I was not able to move the files because /usr/bin/env is not a directory, rather it seems to be some sort of executable java file (usr/bin is a directory).
I am a bit lost and would love some advice on either how to install the module as a working global variable or whether there is another way to run the module without installing it as a global variable.
This will happen if the node.js binary (node) is not installed in the $PATH anywhere.
if you run env node by itself, you will get the same error. It looks like this may be an Ubuntu bug: https://github.com/joyent/node/issues/3911
Try sudo ln -s /usr/bin/nodejs /usr/bin/node - that will symlink the node.js binary from the name Ubuntu gave it to the name it's supposed to have.
EDIT:
As mscdex pointed out in a comment (and as mentioned at the end of the bug I linked), there's a legacy package you can install that should create this symlink.
sudo apt-get install nodejs-legacy
The bug I linked above indicates that there are probably other problems with Ubuntu / Debian's default node.js package, and recommends you install your own either from the PPA mentioned there or from source.
You'll probably need to follow the advice in NPM modules won't install globally without sudo as well.

Categories

Resources