Move a module from devDependencies to dependencies in npm package.json

Npmpackage.json

Npm Problem Overview


Is there any short command to move a module from devDependencies to dependencies in package.json?

I find myself always doing this:

npm uninstall <module_name> --save-dev 
npm install <module_name> --save

Is there a shorter approach to this?

Npm Solutions


Solution 1 - Npm

Shorthand to move from devDependencies to dependencies (prod):

npm i <module_name> -P

If you want to do the opposite (i.e. move a module from dependencies to devDependencies) just do:

npm install <module_name> --save-dev

or shorthand:

npm i <module_name> -D

Solution 2 - Npm

Yes! to move a module from devDependencies to dependencies:

Solution 3 - Npm

In yarn:

Move a module from devDependencies to dependencies:

yarn remove <module_name> --dev && yarn add <module_name> 

Move a module from dependencies to devDependencies :

yarn remove <module_name> && yarn add <module_name> --dev

As said in the comments, the command actually deletes the module and reinstall it in the new place.

Solution 4 - Npm

The problem with using npm or yarn commands is that there is a chance that the version that is re-added is a different version than the one that is currently used. If this is what you want - both a move and an upgrade - then go ahead and use the accepted answer.

If not, simply manually edit your package.json to move the line from the devDependencies object to the dependencies object (creating it if necessary). You can go the other direction too.

The lock file doesn't hold any information about if things are prod or dev dependencies, so that doesn't need to be updated. You can do a npm/yarn install afterwards to fix up any flags in the lock files.

Solution 5 - Npm

The issue of using npm install is that you end up with updated versions. What worked for me is:

  1. Moving them to the intended part (dev, or prod)
  2. Removing them from node_modules folder
  3. Execute npm install

That kept all versions intact.

Solution 6 - Npm

If your project doesn't have a lockfile or a shrinkwrap file yet, you can simply move the corresponding line in your package.json.

(I'm not recommending not using lockfiles)

Solution 7 - Npm

I was trying to find an answer for this question for people that uses Yarn, but it hasn't a command for this matter yet. Although, I believe it is not essential anyway.

Physically (in the Node modules folder) there are no difference between a dependency listed for production and the ones listed for development in your package.json, they'll go to the same place (node_modules).

So, if you need to switch a dependency from devDependencies to dependecies you can go to your package.json and move manually with no need to run a new install or remove the dependency and then install it again with the dev flag.

For me, it's not so great at all to manage the package.json manually, but Yarn is not as advanced as NPM in all functionalities, thus that's a thing to consider.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmad EmamiView Question on Stackoverflow
Solution 1 - NpmSidView Answer on Stackoverflow
Solution 2 - NpmFrancois WoutsView Answer on Stackoverflow
Solution 3 - NpmyohaizView Answer on Stackoverflow
Solution 4 - NpmeedrahView Answer on Stackoverflow
Solution 5 - NpmAhmed MahmoudView Answer on Stackoverflow
Solution 6 - NpmseanView Answer on Stackoverflow
Solution 7 - NpmKhebab-CaseView Answer on Stackoverflow