DEV Community

matoruru
matoruru

Posted on • Edited on

One-liner to install peerDependencies

Here it is:

node -e "console.log(Object.keys(require('./package.json').peerDependencies).join(' '))" | xargs yarn add
Enter fullscreen mode Exit fullscreen mode

How it works

Let's say we have peerDependencies in our package.json like this:

    ...
  },
  "peerDependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}
Enter fullscreen mode Exit fullscreen mode

We'll see one by one:

  1. Load the package.json and get the peerDependencies.

    Welcome to Node.js v15.14.0.
    > require('./package.json').peerDependencies
    { react: '^17.0.2', 'react-dom': '^17.0.2' }
    
  2. Object.keys function collects the keys of the object and put them together in the array.

    Welcome to Node.js v15.14.0.
    > Object.keys({ react: '^17.0.2', 'react-dom': '^17.0.2' })
    [ 'react', 'react-dom' ]
    
  3. join method joins the elements with the given string.

    > [ 'react', 'react-dom' ].join(' ')
    'react react-dom'
    
  4. node -e evaluates the given script.

    $ node -e "console.log('react react-dom')"
    'react react-dom'
    
  5. Pass it to the yarn add with xargs.

    $ node -e "console.log('react react-dom')" | xargs yarn add
    yarn add v1.22.5
    [1/4] Resolving packages...
    [2/4] Fetching packages...
    [########------------------
    

Done 🥳

Top comments (1)

Collapse
 
shinbobbu profile image
shinbobbu

thank you, great job 🥳