How to check if NPM cache already contains a tarball

1.6k Views Asked by At

I am looking at the cache docs: https://docs.npmjs.com/cli/cache

if I ran this:

npm cache add [email protected]

how can I check later, if this is in the npm cache?

I don't see npm cache get [email protected] in the docs...

1

There are 1 best solutions below

4
On

Looks like npm has not a direct way to achieve this, but this script does the trick

create a file cache.js and paste code below

const cacache = require('cacache/en')
const cachePath = require('os').homedir()+'/.npm/_cacache'

cacache.ls(cachePath)
  .then((packages) => {
    for(const i in packages) {
      console.log(packages[i].key)
    }
  }) 

run

npm install cacache

then run

node cache.js | grep lodash

personal opinion: yarn is designed to cache npm packages, if you are going to do that, you could give it a chance

EDIT: I made a script that does all of the above, every feedback is welcome

https://www.npmjs.com/package/npm-check-cache