Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if NPM cache already contains a tarball

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...

like image 930
Alexander Mills Avatar asked Jun 12 '18 07:06

Alexander Mills


People also ask

How do I check my npm cache?

Run: “npm cache verify” for npm version 5 and up As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to be valid. If you want to make sure everything is consistent, use 'npm cache verify' instead.

Can I delete npm cache?

To clear a cache in npm, we need to run the npm cache clean --force command in our terminal. To clear the cache present in npm, you need to run the command. If it doesn't work, run the force clean method since the cache is not cleared simply.

What is npm tarball?

Tarball is a compressed file format. You need to unpack it before running the npm command. From: http://www.rebol.com/docs/unpack-tar-gz.html. To unpack a tar.gz file, you can use the tar command from the shell.

Does npm have a cache?

The npm cache is strictly a cache: it should not be relied upon as a persistent and reliable data store for package data. npm makes no guarantee that a previously-cached piece of data will be available later, and will automatically delete corrupted contents.


1 Answers

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

like image 70
Simone Sanfratello Avatar answered Oct 28 '22 13:10

Simone Sanfratello