315

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?

Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
Emad Emami
  • 6,089
  • 3
  • 28
  • 38
  • If you care about maintaining the same versions in your `package-lock.json`, and merely marking the dependency as a dev dependency, then the approach mentioned in this answer should be the way to go: https://stackoverflow.com/a/57032462/3575560 – solimant Oct 08 '21 at 22:48

8 Answers8

422

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
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sid
  • 14,176
  • 7
  • 40
  • 48
  • 3
    shorthand `npm i module-example -D` will also work to move from prod to dev, as well as `npm i module-example -P` to move from dev to prod dependencies – Andriy Nov 04 '18 at 09:25
  • @Andriy thanks, didn't thought about it. Will update the answer! – Sid Nov 05 '18 at 13:34
  • 2
    If you're using yarn instead, you need to remove it first from dependencies with `yarn remove ` and then re-adding it into devDependencies with `yarn add --dev` – Eugenio Jan 30 '19 at 11:00
  • What If I just move the dependency from `dependencies` to `devDependencies` in `package.json`? The lock file doesn't seem to store any information regarding dev or production – S.j. Sakib Aug 31 '20 at 05:24
  • better to include the version with @ to be sure it will be the same version – Lk77 Feb 23 '21 at 14:32
  • @Eugenio What you are saying is removing & reinstalling it. Is there any way to move dependency from dev to prod in `yarn`? – Anbuselvan Rocky Apr 01 '22 at 07:06
  • This command will not simply "move" package, but rather install package in the desired place (either dependencies or devDependencies). Don't forget to check the version after install - it could be different. – Art713 Oct 28 '22 at 11:33
304

Yes! to move a module from devDependencies to dependencies:

npm install <module_name> --save-prod

Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
Francois Wouts
  • 3,842
  • 1
  • 17
  • 11
57

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.

agilgur5
  • 667
  • 12
  • 30
yohaiz
  • 874
  • 6
  • 11
25

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.

eedrah
  • 2,245
  • 18
  • 32
  • 2
    While there is only a single `dependencies` top-level section in `package-lock.json`, each entry potentially has a `dev` flag to indicate whether it is a dev dependency or a transitive dependency of one. See https://docs.npmjs.com/files/package-lock.json#dev "If [dev is] true then this dependency is either a development dependency ONLY of the top level module or a transitive dependency of one. This is false for dependencies that are both a development dependency of the top level and a transitive dependency of a non-development dependency of the top level." – rob3c Jul 23 '19 at 00:53
  • I wish I saw this answer first. Don't forget that updating to a new version potentially introduces breaking changes - as I forgot myself. – Forrest Wilkins Jan 30 '23 at 00:55
6

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.

Ahmed Mahmoud
  • 1,479
  • 2
  • 12
  • 18
3

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)

sean
  • 877
  • 10
  • 16
0

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.

0

For Yarn, I moved them manually inside package.json and then ran yarn install. Yarn lock file does not get updated, so I think this is fine.

Emre
  • 831
  • 11
  • 13