I have one question I think is pretty simple. Could you explain me why sometimes package.json is automatically updates and sometimes don't. e.g. if I installed express or gulp these plugins were added to dependencies automatically. But if I installed express-jwt or mongodb these plugins were not added
They are added to package.json when you append one of the following flags:
-S|--save|-D|--save-dev|-O|--save-optional
See https://docs.npmjs.com/cli/install for full documentation.
If we use the following command to install npm dependencies-
npm install express-jwt --save
or
npm install mongodb --save
these installed dependencies will also be added to package.json file.
When you install a package with the --save or --save-dev flag, these are persisted to the dependencies or devDependencies arrays respectively.
If no flag is added, the package is downloaded but not persisted to package.json
Edit: There's is also the more obscure --save-optional flag, but it'd say it's mostly out of scope for this particular case.
Related
I am looking to update the following NPM (v5) dependency in my application from version 1.0.0 to 1.0.1 without any change to my package.json file.
"dependencies": {
"package": "~1.0.0"
},
My current package-lock.json file references the dependency as version 1.0.0, so as expected, running npm install installs version 1.0.0 of the package.
The issue lies when running either npm install package#1.0.1 or npm update package where both commands seem to change how the package version reference in package.json
Is there a single command I can run to achieve a minor version update to only the package-lock.json file?
Thanks in advance!
Run npm update <package>.
This will update it to the latest version that satisfies the requirements specified in your package.json and reflect the update in the package-lock.json.
package-lock.json is generated by npm and it's difficult to modify without npm since it contains package hashes.
If you're only referencing modules using the patch version (~1.0.0) I think it's safe to do the following:
Backup package.json and delete it
Run npm update package. This will use package-lock.json as a reference and will also update package-lock.json
Restore package.json
Running npm update package now will not update package.json since package-lock.json is the newer version
If you're looking for a one line command:
mv package.json package.json.tmp && npm update package && mv package.json.tmp package.json
Again, this is safe to do only when dealing with patch versions (~1.0.0). If you specify minor (^1.0.0) or major (1.0.0) versions you may want to update package.json directly.
How do I yarn add without update other packages?
When I use yarn add react-native-charts-wrapper this command also updates my other packages.
But I don't want to update other packages.
It seems someone or you were modified your package.json's dependencies manually like: change version of package by hand or etc...
You can fix this by deleting yarn.lock and type yarn install (installs all packages on package.json)
I just switched to gulp task runner to automate my workflow, but there is this problem whenever i want to start a new project i have to install all packages required in gulpfile.js using the following command:
npm install --save-dev {package name}
Imagine there are 20 of them, it's a bit boring. How can simplify this?
You can add multiple package names to npm install:
npm install --save-dev package1 package2 package3
npm will install and save the specified packages in your package.json.
Personally I use mostly the same gulp plugins for all of my projects. I copy the devDependencies bit from the package.json of one of my previous projects into my newly created package.json, then I run npm i which installs all dependencies listed in package.json. It's a huge timesaver, especially since I usually copy my gulpfile.js as well.
Note: don't forget to run npm outdated if it's been a while since your previous project started, to check if any of the dependencies have been updated in the meantime.
You can also use brace expansion for installing many similarly named packages:
npm i -D babel-{core,preset-es2015,preset-react}
Guys I know that using npm install -g we can install the node modules/packages globally, but I am not sure about the options --save and --save-dev
I have Googled it but still not clear about it. Please share your thoughts.
--save adds the third-party package to the package's dependencies. It will be installed together with the package whenever someone runs npm install yourPackage.
--save-dev adds the third-party package to the package's development dependencies. It won't be installed when someone installs your package. It's typically only installed if someone clones your source repository and runs npm install in it.
Dev dependencies, as the same suggests, are those dependencies that are only needed for developing the package. That can include test runners, compilers, packagers, etc.
Both types of dependencies are stored in the package's package.json file. --save adds to dependencies, --save-dev adds to devDependencies. From the documentation:
devDependencies
If someone is planning on downloading and using your module in their program, then they probably don't want or need to download and build the external test or documentation framework that you use.
In this case, it's best to map these additional items in a devDependencies object.
These things will be installed when doing npm link or npm install from the root of a package, and can be managed like any other npm configuration param. See npm-config(7) for more on the topic.
For build steps that are not platform-specific, such as compiling CoffeeScript or other languages to JavaScript, use the prepublish script to do this, and make the required package a devDependency.
Edit: As of npm 5.0.0 the installed modules are added as a dependency by default, so the --save option is no longer needed.
--save-dev is used to save the package for development purpose. Example: unit tests, minification.
--save is used to save the package required for the application to run.
--save-dev will save npm modules in development dependencies in package.json i.e it will save inside devDependencies object.
--save will save npm modules dependencies in package.json i.e it will save inside dependencies object.
I'm new and I need to use node for off-line use so, I'm trying to understand how the install modules work.
It's the same if I use npm install express or included it in the package.json?
The way a package is installed will be the same whether you manually type npm install express or put it in your package.json and then do npm install. The difference comes when you try to install your Node project elsewhere.
For example, if your code was checked into GitHub and you didn't include a package.json with all of the dependencies listed, then when the project was downloaded you would have to manually re-install all of the dependencies on the command line in order for it to work. But if you had checked in a package.json with the code, then you could run npm install to install all of the dependencies at once, and not have to remember which ones were necessary.
In addition, the package.json allows you to specify an "approximate version" of a dependency to use. This way if a few packages in your project share a dependency and they all specify similar "approximate versions", only one version will be installed and it will be shared between packages. This saves some install time.
Nothing actually. But you don't want to do that again and again. So you might as well put your module dependencies in your package.json