One-liner to install peerDependencies

The command is:

node -e “console.log(Object.keys(require(‘./package.json’).peerDependencies).join(‘ ‘))” | xargs yarn add

How it works

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


},
“peerDependen…


This content originally appeared on DEV Community and was authored by matoruru

The command is:

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

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"
  }
}

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 ?


This content originally appeared on DEV Community and was authored by matoruru


Print Share Comment Cite Upload Translate Updates
APA

matoruru | Sciencx (2021-04-08T20:39:05+00:00) One-liner to install peerDependencies. Retrieved from https://www.scien.cx/2021/04/08/one-liner-to-install-peerdependencies/

MLA
" » One-liner to install peerDependencies." matoruru | Sciencx - Thursday April 8, 2021, https://www.scien.cx/2021/04/08/one-liner-to-install-peerdependencies/
HARVARD
matoruru | Sciencx Thursday April 8, 2021 » One-liner to install peerDependencies., viewed ,<https://www.scien.cx/2021/04/08/one-liner-to-install-peerdependencies/>
VANCOUVER
matoruru | Sciencx - » One-liner to install peerDependencies. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/08/one-liner-to-install-peerdependencies/
CHICAGO
" » One-liner to install peerDependencies." matoruru | Sciencx - Accessed . https://www.scien.cx/2021/04/08/one-liner-to-install-peerdependencies/
IEEE
" » One-liner to install peerDependencies." matoruru | Sciencx [Online]. Available: https://www.scien.cx/2021/04/08/one-liner-to-install-peerdependencies/. [Accessed: ]
rf:citation
» One-liner to install peerDependencies | matoruru | Sciencx | https://www.scien.cx/2021/04/08/one-liner-to-install-peerdependencies/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.