how to refresh package.json file after install a new module? - node.js

when the package.json file already exists and then install a new node module, how to add the new dependency data (module name and version number) to the package.json file?
I mean, not manually. Is there a native method using npm ?

As of npm 5.0.0 the dependancy should be automatically created on the file, prior to this version you must pass option --save when doing npm install to tell npm to write the package.json.

If you are adding any module as dependency module in you project, you can use this command npm install module_name --save it automatically add in package.json file

Related

npm (5.4.*) module - How to disable node_modules removal if not defined in package.json

I am using npm versioned 5.4.2, I have some node_modules, now from 3.x npm module behavior is if the modules inside node_modules directory are not defined in package.json, then npm deletes the modules in node_modules.
how to override this behavior, like I do not want to remove the npm modules already installed even if they are not defined in package.json
If new module is added using "npm install", I do not want to overwrite package.json file.
How to achieve this? Is there any flag in .npmrc?
With regards,
-M-
Not the answer, but still... : https://github.com/npm/npm/issues/16853
npm install package --no-save

How to add dependency to package.json later

In my application I have installed few node modules using below command
npm install <modulename>
I forgot to mention "--save" to save the dependency list to package.json file.
Now I would like to update those dependencies in package.json file without updating the file manually . Any idea how it can be done ?
You can run the same command again, specifying --save flag and it will be automatically included in package.json. The only problem is that the version of the package can be updated to newer version, so you may specify the specific version of your app: npm i --save app#1.0.1.
Alternatively you can modify package.json yourself to include the dependency:
"dependencies": {
"module": "*"
}
Just do: npm i name_of_package -S or the long version: npm install package_name --save
If you need to save it as a dev dependency, use the -D flag

Update this module to the last version

I'm inside a folder
c:\nodeProject\node_modules\express>
Now, i want to update the module here, to the latest version.
I want NPM to check the package.json, and update this module.
What is the command line to do it?
npm uninstall express
npm install express#latest --save
That's what your looking for ?

Automatically Update the package.json in NodeJS application

I have created the package.json using npm init. After some stage I installed few more modules as per the need.Rather than updating the package.json dependencies manually, is there any way to automatically update it??
If I understand what you are asking correctly, you want to use npm to install a dependency and have that dependency automatically added to your package.json ?
If so, use --save after the package name :
npm install [package name] --save
For download the all packages configured in the package.json, use:
npm update --save
Docs: https://docs.npmjs.com/cli/update

npm install --save, what is the use of not saving

I understand the differences between npm install something and npm install something --save (for anyone wondering, the first one will install the dependency only while the latter will install the dependency and add it to your package.json).
However I do not understand why there is a --save option in the first place. In other words, why would you ever want to install a dependency without adding it to your package.json file? Why is the --save option not default?
A lot of websites/npm modules/SaaS suggest installing their module using npm install something (newrelic is one of them for instance), am I missing something?
Edit: Starting from NPM 5, --save is now on by default.
You would have a scenario such as you need some module to install without adding dependency to package.json file, for ex. you just want to try some module, and not sure you would be really using that module in production or while deploying, so instead adding the module dependency to package.json, just give it a try without using --save. this is why npm install without --save exists.
But For most of your modules you might require using --save, for ex. npm install express --save,
in this case you surely know that you are going to use express for you application.
The other scenario, for not using --save, would be, npm install heapdump or npm install nodemon, I would use it for testing my apps performance, but not add a dependency in the package.json :)
Also, As #surajck said in comment below: when you are doing global installs, in that case adding dependencies using --save, to the package.json would not make sense.
I just learned a nice trick from Jonathan Mills' JavaScript Best Practices course on Pluralsight. From the terminal:
npm config set save=true
Now I don't need to remember --save anymore. And I also now use
npm config set save-exact=true
Because I want the exact version of the package not the ^ prefix.
By default with version npm 5.0+ npm install adds the module to the dependencies list in the package.json file; with earlier versions of npm, you must specify the --save option explicitly. Then, afterwards, running npm install in the app directory will automatically install modules in the dependencies list.

Resources