Will npm install global look in the local package.json? - node.js

Say I have "karma": "~0.12.0" in package.json, and the latest version of karma is 0.13.19. Will npm install -g karma look in the local package.json or will it install the latest version of karma available in npm?

Once you provide npm with a package name, it will not look for in the package.json file.
If you want specific version, you can provide it in the command, not in the package.json:
npm install -g karma#0.12.0

Related

Gulp install globally

I need install gulp. I used this command: npm install gulp gulp-plumber gulp-sass gulp-autoprefixer gulp-concat gulp-uglify gulp-notify
Unfortunelly in my project It was created node_modules directory. How do I do this? I don't have to add files in any project. I would like to use only command "gulp watch".
add -g for global installing
$ npm install -g gulp
OR
$ sudo npm install -g gulp
To install a npm package globally on a machine, use one of
npm i -g <packages>
npm install --global <packages>
However, this doesn't let anybody else know about this dependency. Consider instead adding node_modules/ to your .gitignore file (or the ignore of whatever VCS you are using), then install with
npm i --save-dev <packages>
The --save-dev here means that it knows that this dependency is only for development and not required in deployment. So if you use a script to deploy which installs the npm dependencies, you can have it ignore these packages
Add gulp in your package.json and run npm intall from your project directory.

npm install doesn't save dependency to package.json

It does add only when I execute: npm install <package_name> --save
In the documentation though: https://docs.npmjs.com/cli/install is written this:
By default, npm install will install all modules listed as dependencies in package.json.
Which is misleading.
npm install without arguments installs all dependencies (and dev dependencies) listed in the package.json file.
npm install --production installs all the dependencies (but no dev dependency)
npm install <package> installs a package and its dependencies.
npm install <package> --save installs a package and its dependencies, and adds it in the package.json file.
Edit: Since npm 5, --save is implied.
No, it's not. I think you are misreading the sentence.
npm install without any package name (as in your quote) will install all dependencies mentioned in the package.json.
Crete package.json file in your application folder.
Exp:- var/www/html/node/rest/package.json
{
"name": "node-api",
"main": "server.js",
"version": "0.0.1",
"dependencies": {
"express": "~4.0.0",
"mongoose": "~3.6.13",
"body-parser": "~1.0.1",
"mysql": "^2.5.4"
}
}
Then run npm install command your application folder
Exp:- var/www/html/node/rest npm install
You could run npm init again to update the dependencies info.

Node install npm modules

I'd like to install npm modules based on package.json
I'd like to know if there is anyway to automate installing modules.
For instance dependencies of package.json is as follows.
"dependencies": {
"express": "3.1.0",
"jade": "*",
"stylus": "*",
"mongodb": ">= 0.9.6-7"
}
do I have to install modules one by one like this?
npm install express#3.1.0
npm install mongodb#0.9.6
and etc.
Any help would be appreciate.
See the documentation about npm install.
By default, npm install will install all modules listed as dependencies in package.json.
So you can just type npm install.
when you are installing first time use --save, that module installation info will be added to package json
after that at new location you just need to run npm install
npm install express#3.1.0 --save
npm install mongodb#0.9.6 --save
npm install
also refer link
You can do this by typing:
sudo apt-get update
sudo apt-get install npm
use nvm to switch versions.
If you want to install a specific version of module you should use
npm install module_name#version --save
--save add's the module and the version of the module to your package.json file's dependencies. If you want to install just any version of a module you can use
npm install module_name --save
if you don't use --save at the and node would still install the last version of the module you want but it wouldn't add it to your package.json file . In this case you have some specific versions of some modules in your package.json file if you want to install them, you can simply use the
npm install
command. npm install installs all modules in your package.json file.
Also if you are new in nodeJs you can check this out. I hope this helps. Have a good day good sir.
You could install modules written in package.json as follows.
npm install

How to install a previous exact version of a NPM package?

I used nvm to download node v0.4.10 and installed npm to work with that version of node.
I am trying to install express using
npm install express -g
and I get an error that express requires node version >= 0.5.0.
Well, this is odd, since I am following the directions for a node+express+mongodb tutorial here that used node v0.4.10, so I am assuming express is/was available to node v0.4.10. If my assumption is correct, how do I tell npm to fetch a version that would work with my setup?
If you have to install an older version of a package, just specify it
npm install <package>#<version>
For example: npm install express#3.0.0
You can also add the --save flag to that command to add it to your package.json dependencies, or --save --save-exact flags if you want that exact version specified in your package.json dependencies.
The install command is documented here: https://docs.npmjs.com/cli/install
If you're not sure what versions of a package are available, you can use:
npm view <package> versions
And npm view can be used for viewing other things about a package too. https://docs.npmjs.com/cli/view
It's quite easy. Just write this, for example:
npm install -g npm#4.6.1
Or:
npm install -g npm#latest // For the last stable version
npm install -g npm#next // For the most recent release
First remove old version, then run literally the following:
npm install express#3.X
or
npm install express#4.X
and for stable or recent
npm install -g npm#latest // For the last stable version
npm install -g npm#next // For the most recent release
In my opinion that is easiest and fastest way:
$ npm -v
4.2.0
$ npm install -g npm#latest-3
...
$ npm -v
3.10.10
you can update your npm package by using this command:
npm install <package_name>#<version_number>
example:
npm install yargs#12.0.2
You can use the following command to install a previous version of an npm package:
npm install packagename#version
I have a general way to solve this type of problems, which could be helpful too, especially when cloning repositories to run them locally, but requires a little more analysis of the versions.
With the package npm-check-updates I verify the versions of the packages (according to the package.json file) that are not declared in their latest available versions, as shown in the figure (https://www.npmjs.com/package/npm-check-updates):
With this information we can verify the update status of the different packages and make decisions as to which packages to upgrade / degrade and which ones do not.
Assuming that we decided to update all the packages as they are listed, we can use the ncu -u command which only modifies your package.json file. Run npm install to update your installed packages and package-lock.json.
Then, depending on the requirements of the repository, we can refine what is needed, installing the specific versions with
npm view <package> versions and npm install <package>#<version>
The easiest way I found: add package name with the version in package.json and then run npm install
"next-seo": "^5.4.0",
"next-themes": "^0.1.1",
"nextjs-progressbar": "^0.0.14",
If you have to install an older version of a package, just specify it
npm install #
For example: npm install express#3.0.0
You can also add the --save flag to that command to add it to your package.json dependencies, or --save --save-exact flags if you want that exact version specified in your package.json dependencies.
The install command is documented here: https://docs.npmjs.com/cli/install
If you're not sure what versions of a package are available, you can use:
npm view versions
And npm view can be used for viewing other things about a package too. https://docs.npmjs.com/cli/view
Use npm config set save-exact=true if you want to install the exact version

npm install vs. update - what's the difference?

What is the practical difference between npm install and npm update? When should I use which?
The difference between npm install and npm update handling of package versions specified in package.json:
{
"name": "my-project",
"version": "1.0", // install update
"dependencies": { // ------------------
"already-installed-versionless-module": "*", // ignores "1.0" -> "1.1"
"already-installed-semver-module": "^1.4.3" // ignores "1.4.3" -> "1.5.2"
"already-installed-versioned-module": "3.4.1" // ignores ignores
"not-yet-installed-versionless-module": "*", // installs installs
"not-yet-installed-semver-module": "^4.2.1" // installs installs
"not-yet-installed-versioned-module": "2.7.8" // installs installs
}
}
Summary: The only big difference is that an already installed module with fuzzy versioning ...
gets ignored by npm install
gets updated by npm update
Additionally: install and update by default handle devDependencies differently
npm install will install/update devDependencies unless --production flag is added
npm update will ignore devDependencies unless --dev flag is added
Why use npm install at all?
Because npm install does more when you look besides handling your dependencies in package.json.
As you can see in npm install you can ...
manually install node-modules
set them as global (which puts them in the shell's PATH) using npm install -g <name>
install certain versions described by git tags
install from a git url
force a reinstall with --force
npm install installs all modules that are listed on package.json file and their dependencies.
npm update updates all packages in the node_modules directory and their dependencies.
npm install express installs only the express module and its dependencies.
npm update express updates express module (starting with npm#2.x, it doesn't update its dependencies).
So updates are for when you already have the module and wish to get the new version.
In most cases, this will install the latest version of the module published on npm.
npm install express --save
or better to upgrade module to latest version use:
npm install express#latest --save --force
--save: Package will appear in your dependencies.
More info: npm-install
npm update: install and update with latest node modules which are in package.json
npm install: install node modules which are defined in package.json(without update)
Many distinctions have already been mentioned. Here is one more:
Running npm install at the top of your source directory will run various scripts: prepublish, preinstall, install, postinstall. Depending on what these scripts do, a npm install may do considerably more work than just installing dependencies.
I've just had a use case where prepublish would call make and the Makefile was designed to fetch dependencies if the package.json got updated. Calling npm install from within the Makefile would have lead to an infinite recursion, while calling npm update worked just fine, installing all dependencies so that the build could proceed even if make was called directly.

Resources