Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
A lazy local cache for npm
Here are all the ways in which npm_lazy is resilient to registry failures:
cacheAge
(default: 1 hour), we will attempt to contact the registry first. However, if contacting the registry fails, then the old version of the metadata is sent instead. This means that even when outages occur, you can install any package that has been installed at least once before.maxRetries
times. The error response content and error status code are also now returned up from the registry to the npm_lazy clients.Added support for using a http proxy (note: not a Socks5 proxy). This can be configured either via the config file or via the http_proxy
environment variable, see the config at the end for an example. Thanks @migounette! As I am not using a proxy myself, please report any issues via GH (pull requests welcome!).
Note: if you already have a proxy for npm, make sure you don't run into an issue where npm uses the proxy when accessing npm_lazy. You don't want to have npm install -> proxy -> npm_lazy -> proxy
, but rather npm install -> npm_lazy -> proxy
since your proxy probably doesn't know how to connect to npm_lazy. You will need to disable npm's internal proxy config, see this comment for the details.
Check out the changelog for version history.
Requires node >= 0.10.x
v1.1.x adds a command called npm_lazy
to make things even easier. Install via npm:
sudo npm install -g npm_lazy
To start the server, run:
npm_lazy
To edit the configuration, start by initializing a file from the default config file:
npm_lazy --init > ~/npm_lazy.config.js
To start the server with a custom configuration:
npm_lazy --config ~/npm_lazy.config.js
Make sure you also empty out any npm caches by running npm cache clean
, as npm does its own local caching, which means that some files might still point directly to the registry rather than to the npm_lazy endpoints.
Or alternatively, if you don't want to install this globally, you can just clone the repo: git clone git@github.com:mixu/npm_lazy.git && cd npm_lazy && npm install
and edit config.js
.
To temporarily set the registry:
npm --registry http://localhost:8080/ install socket.io
To permanently set the registry via command line:
npm config set registry http://localhost:8080/
To permanently set the registry via config file, in ~/.npmrc:
registry = http://localhost:8080/
For more info, see "npm help config" and "npm help registry".
A few things that might be useful to know:
npm cache clean
so that your local npm command will request every package you want at least once from npm_lazy.~/.npm_lazy
.npm install
for cached packages but more exotic npm endpoints will not work if the registry is down; they will simply act like their non-npm_lazy equivalents).cacheAge
to 0
so that npm metadata is always refreshed because npm wants to know that you have the most recent package _id
before it allows writing. This will still return cached data for package.json indexes needed for installation if the registry is down, but only after attempting to contact the registry (this seems like a decent, but not perfect compromise).cacheAge
while retaining all cached artifacts.npm shrinkwrap
may interfere with it since they may contain direct references to registry.npmjs.com. Make sure you clean up that stuff.rejectUnauthorized
to false in the config.First, install a package successfully so that it is cached.
Next, to simulate a network failure, add 0.0.0.1 registry.npmjs.com
to /etc/hosts
and try installing that same package again (in another folder). You should see something like this:
npm_lazy at localhost port 8080
npm_lazy cache directory: /home/m/.npm_lazy
Fetch failed (1/5): https://registry.npmjs.com/socket.io { [Error: connect EINVAL] code: 'EINVAL', errno: 'EINVAL', syscall: 'connect' }
Fetch failed (2/5): https://registry.npmjs.com/socket.io { [Error: connect EINVAL] code: 'EINVAL', errno: 'EINVAL', syscall: 'connect' }
Fetch failed (3/5): https://registry.npmjs.com/socket.io { [Error: connect EINVAL] code: 'EINVAL', errno: 'EINVAL', syscall: 'connect' }
Fetch failed (4/5): https://registry.npmjs.com/socket.io { [Error: connect EINVAL] code: 'EINVAL', errno: 'EINVAL', syscall: 'connect' }
Fetch failed (5/5): https://registry.npmjs.com/socket.io { [Error: connect EINVAL] code: 'EINVAL', errno: 'EINVAL', syscall: 'connect' }
[OK] Reusing cached result for https://registry.npmjs.com/socket.io
Configured by editing config.js
in the same directory:
var path = require('path'),
homePath = path.normalize(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']);
module.exports = {
// Logging config
loggingOpts: {
// show the ip address of the machine requesting the npm package
logRequesterIP: true,
// Print to stdout with colors
logToConsole: true,
// Write to file
logToFile: false,
// This should be a file path.
filename: homePath + '/npm_lazy.log'
},
// Cache config
// `cacheDirectory`: Directory to store cached packages.
//
// Note: Since any relative path is resolved relative to the current working
// directory when the server is started, you should use a full path.
cacheDirectory: homePath + '/.npm_lazy',
// `cacheAge`: maximum age before an index is refreshed from remoteUrl
// - negative value means no refresh (e.g. once cached, never update the package.json metadata)
// - zero means always refresh (e.g. always ask the registry for metadata)
// - positive value means refresh every n milliseconds
// (e.g. 60 * 60 * 1000 = expire metadata every 60 minutes)
//
// Note: if you want to use `npm star` and other methods which update
// npm metadata, you will need to set cacheAge to 0. npm generally wants the latest
// package metadata version so caching package metadata will interfere with it.
// Recommended setting: 0
cacheAge: 0,
// Request config
// max milliseconds to wait for each HTTP response
httpTimeout: 10000,
// maximum number of retries per HTTP resource to get
maxRetries: 5,
// whether or not HTTPS requests are checked against Node's list of CAs
// set false if you are using your own npm mirror with a self-signed SSL cert
rejectUnauthorized: true,
// Remote and local URL
// external url to npm_lazy, no trailing /
externalUrl: 'http://localhost:8080',
// registry url with trailing /
remoteUrl: 'https://registry.npmjs.com/',
// bind port and host
port: 8080,
host: '0.0.0.0',
// Proxy config
// You can also configure this using the http_proxy and https_proxy environment variables
// cf. https://wiki.archlinux.org/index.php/proxy_settings
proxy: {
// http: 'http://1.2.3.4:80/',
// https: 'http://4.3.2.1:80/'
}
};
When a resource is requested:
FAQs
Lazy local npm cache server
The npm package npm_lazy receives a total of 16 weekly downloads. As such, npm_lazy popularity was classified as not popular.
We found that npm_lazy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.