9

Using yarn, I have added few additional libraries ( let say jquery).

yarn add jquery

This will be added by default to dependencies block in package.json

but I want to change its location from dependencies to the devDependencies block.

What I currently do is:

  • remove library

yarn remove jquery

  • then add again with -D

yarn add jquery -D

So I am looking for any command in yarn or npm which directly changes the library location from dependencies to the devDependencies block without uninstalling and reinstalling the same.

Community
  • 1
  • 1
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • this solution given in context of npm does not work: https://stackoverflow.com/questions/46903002/move-a-module-from-devdependencies-to-dependencies-in-npm-package-json – xkeshav Jul 13 '18 at 19:01

1 Answers1

8

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 in this question.

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.

Because you asked specifically for a command:

Choose your favorite combination out of the command line text editors: sed, awk, grep, perl or even python. Or you could use a JSON editor like jq.

You are right though - there should be a native command for this and perhaps we need to make a pull request to npm/yarn.

eedrah
  • 2,245
  • 18
  • 32
  • simply moving dependencies in `package.json` will cause issues when installing dependencies across environments. `package-lock.json` does in fact hold information about whether or not a dependency is prod or dev – Stephen Watkins Mar 17 '23 at 17:50