npm install -g *no arguments* - npm

What does the command npm install -g (no arguments) do? I opened npm docs but didn't get it:
In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
What does it mean?

What it means is that whatever project currently in, npm will take the dependencies from the package.json in your current project folder and then install those dependencies globally.

Related

npm 5 install folder without using symlink

Before publishing my node library, I could use the advice the npm documentation wrote about:
To test a local install, go into some other folder, and then do:
cd ../some-other-folder
npm install ../my-package
Prior to version 5 of npm, I had no problem as it produce what I expected, ie a folder with the output of what I will publish.
However, using npm 5, it now creates a symlink to my local project as described in the npm documentation:
npm install :
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
How can I use the "old" way to install local project? Or is there a new way to check if my library is correct?
Thank you.
Use npm pack + npm install (as suggested by install-local package)
npm pack <path-to-local-package>
npm install <package-version.tgz>
This will effectively copy your local package to node_modules.
Note that this will package only production relevant files (those listed in the files section of your package.json). So, you can install it in a test app under the package own directory. Something like this:
my-package
package.json
test
test-app
package.json
node_modules
my-package
Assuming that test dir is not included in the files in my-package/package.json.
This works the same way with npm 5 and older versions.
I wrote npm-install-offline which allows you to install npm packages from a local repository or folder. By default it copies the folder on install but you can also choose to symlink.
https://www.npmjs.com/package/npm-install-offline
npx npm-install-offline ../some-package
Or
npx npm-install-offline my-npm-package --repo ./my-offline-npm
It also will install the package dependencies which npm does not do with local packages.

npm global package(ember-cli) install as devDependency in project

If I npm install -g ember-cli#2.7.0, then in my project, if I do npm install --save-dev ember-cli#2.7.0, shouldn't the global ember-cli be copied over to my project right away, and just bump up version number in package.json?
Am I right?
Technically it will use the cached version (in the npm_cache folder) to do the install, not the copy that you installed globally. As for the version number, it should update your package.json file to "ember-cli": "^2.7.0"

How to change npm path

All my npm packages work, but my npm package list shows empty. I am sure this is issue with a path but not sure how to fix it.
Which gulp gives me >
[~] ruby-2.2.3 $ which gulp
/usr/local/bin/gulp
Which npm gives me >
[~] ruby-2.2.3 $ which npm
/usr/local/bin/npm
npm list gives me >
[~] ruby-2.2.3 $ npm list
/Users/kimmo
└── (empty)
It looks like you are confusing packages that are installed globally with those locally. The paths for gulp and npm look like the global install locations. Packages you install locally will be found under a node_modules folder in the root of your project.
You can confirm this by comparing the results from:
npm ls -g --depth=0
npm ls --depth=0
The first command will show the globally installed packages. The second will show the local packages.
At the top of the resulting output, each shows the directory where the global or local install is located.
Finally, the --depth=0 flag only shows the packages that were required and not the dependencies of those packages (and those packages, etc). I find that most of the time, that's all I care about. If you agree, you can easily make this a default with npm config set depth 0 or by editing your .npmrc file in your home directory.
So! My guess is that you installed gulp with the -g flag but you haven't installed anything locally (with no flag, so to speak). That's why there's a difference between what which is showing and npm ls is showing.

What do the --save flags do with npm install

I see instructions to install a package with either
npm install <package_name>
or
npm install <package_name> --save
or
npm install <package_name> --save-dev
What is the difference between these options?
Updated, 2019:
Since this question was asked there was a change to npm, such that --save has become the default option, so you do not need to use --save to update the dependencies.
Original Answer:
npm install <package_name> --save installs the package and updates the dependencies in your package.json.
npm install <package_name> --no-save installs the package but does not update the dependencies as listed in your package.json.
npm install <package_name> ---save-dev updates the devDependencies in your package. These are only used for local testing and development.
You can read more at https://docs.npmjs.com/getting-started/using-a-package.json.
npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:
-S, --save: Package will appear in your dependencies.
-D, --save-dev: Package will appear in your devDependencies.
-O, --save-optional: Package will appear in your optionalDependencies.
When using any of the above options to save dependencies to your package.json, there is an additional, optional flag:
-E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm's default semver range operator.
Further, if you have an npm-shrinkwrap.json then it will be updated as well.
<scope> is optional. The package will be downloaded from the registry associated with the specified scope. If no registry is associated with the given scope the default registry is assumed. See npm-scope.
Note: if you do not include the #-symbol on your scope name, npm will interpret this as a GitHub repository instead, see below. Scopes names must also be followed by a slash.
Examples:
npm install sax --save
npm install githubname/reponame
npm install #myorg/privatepackage
npm install node-tap --save-dev
npm install dtrace-provider --save-optional
npm install readable-stream --save --save-exact
Note: If there is a file or folder named <name> in the current working directory, then it will try to install that, and only try to fetch the package by name if it is not valid.
(from official docs) https://docs.npmjs.com/cli/install
The --save flag no longer serves a purpose.
Previously, as the other answers noted, the --save flag would update the dependencies in the project's package.json file, but npm install now includes this functionality by default.
At this point if you want to prevent npm install from saving dependencies, you have to use the --no-save flag.
Thanks to Coruscate5 for mentioning this in their comment.
More info in the npm-install documentation:
npm install saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with some additional flags:
-P, --save-prod: Package will appear in your dependencies. This is the default unless -D or -O are present.
-D, --save-dev: Package will appear in your devDependencies.
-O, --save-optional: Package will appear in your optionalDependencies.
--no-save: Prevents saving to dependencies.
When using any of the above options to save dependencies to your package.json, there are two additional, optional flags:
-E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm’s default semver range operator.
-B, --save-bundle: Saved dependencies will also be added to your bundleDependencies list.

package.json for global modules - not dependencies

I would like to list some modules that I want to install globally - they are not project related (things like bower, npm-check-updates, ... which I use all the time). Is it possible to manage this with a "global" package.json, or do I need to maintain a separate shell script to perform this installation? Currently I am doing:
# global-npm-pacakges.sh
npm install -g npm-check-updates#1.5.1
npm install -g bower#1.4.1
Any other way of doing this?
Your way is fine but that means you also need to maintain another script to
uninstall all of them globally
Using npm install -g with a global package.json, you still have to manage a clean up script.
There is no npm uninstall -g based on the package.json.
I think you need to
npm uninstall name<#version> or
npm -R name<#version>
Doing npm -R node_modules is not gracefully way of doing it.
It does not clean up the /tmp/xxx
There is some suggestions of using a symlink in this threads for your references:
How do I install a module globally using npm?