npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

check-runtime-env

v0.4.0

Published

Node.js implementation of a checker for some runtime environment properties

Downloads

7

Readme

check-runtime-env / check-runtime-env.js

NPM Version NPM Downloads Code Style Coverage Status license - APACHE-2.0

Node.js implementation of a checker for some runtime environment properties.

The purpose of this library is to simplify some check in the environment where the code is running (at runtime).

Usage

Get a reference to the library:

// Node.js example
const assert = require('assert').strict

// reference the library, not needed if using destructuring assignment, see below
const RuntimeEnvChecker = require('../src/') // from local path
assert(RuntimeEnvChecker !== null)

get some runtime data and version constraints, optional:

const nodeVersion = process.version
assert(nodeVersion !== null)
const npmVersion = RuntimeEnvChecker.getVersionOfNpm()
assert(npmVersion !== null)
const engines = require('../package.json').engines
assert(engines !== null)
const expectedNodeVersion = engines.node
assert(expectedNodeVersion !== null)
const expectedNPMVersion = engines.npm
assert(expectedNPMVersion !== null)

console.log(`Node.js current version: ${nodeVersion}`)
console.log(`Node.js version expected in 'package.json': ${engines.node}`)
console.log(`NPM current version: ${npmVersion}`)
console.log(`NPM version expected in 'package.json': ${engines.npm}`)

call some static method exposed by the class:

console.log(`Doing some tests; note that a check not satisfied will throw Error ...`)
console.log(`Check version of Node, using defaults, success: ${RuntimeEnvChecker.checkVersionOfNode()}`)
console.log(`Check version of Node, using explicit values, success: ${RuntimeEnvChecker.checkVersionOfNode(nodeVersion, expectedNodeVersion)}`)
console.log(`Check version of NPM, using NPM release found, and NPM expected value implicit, success: ${RuntimeEnvChecker.checkVersionOfNpm(npmVersion)}`)
console.log(`Check version of NPM, using explicit values, success: ${RuntimeEnvChecker.checkVersionOfNpm(npmVersion, expectedNPMVersion)}`)
console.log(`Check version (generic), using explicit values, success: ${RuntimeEnvChecker.checkVersion('10.13.0', '>=8.9.0 <12.0.0')}`)
try {
  console.log(`Sample failure in check version (generic): expected error`)
  console.log(`Check version (generic), using explicit values, success: ${RuntimeEnvChecker.checkVersion('10.13.0', '>=12.0.0')}`)
} catch (e) {
  console.log(e)
}
console.log(`Tell the given version '10.13.0', if it's compatible with the constraint '>=12.0.0': ${RuntimeEnvChecker.isVersionCompatible('10.13.0', '>=12.0.0')}, but anyway no error raised here`)

console.log(`Check that the given string is not empty (generic), success: ${RuntimeEnvChecker.checkStringNotEmpty('10.13.0')}`)
console.log(`Node.js environment is: '${process.env.NODE_ENV}'`)
console.log(`Node.js environment from the library is: '${RuntimeEnvChecker.getNodeEnv()}'`)
console.log(`Node.js environment is defined: ${RuntimeEnvChecker.isEnvVarDefined('NODE_ENV')}`)
console.log(`Node.js environment is production: ${RuntimeEnvChecker.isNodeEnvProduction()}`)
// console.log(`Check that Node.js environment is production: ${RuntimeEnvChecker.checkNodeEnvProduction()}`)

console.log('Ensure JavaScript strict mode is enabled (or an Exception will be thrown):')
const checkSafeMode = RuntimeEnvChecker.checkStrictMode()
console.log(`strict mode enabled : ${checkSafeMode}`)

console.log(`No more tests.`)

Look into the example folder for more sample scripts that uses the library (inline but it's the same using it from npm registry).

A sample usage could be to check if current Node.js release satisfies the release wanted, and if not throw an Error (or instead log a Warning).

Requirements

Node.js 14 LTS (14.15.0) or later; NPM 6.8.0 or later.

Sources

Source code is all inside main repo: check-runtime-env.js.

Documentation generated from source code (library API): here.

Note

The library exposes some static methods useful at runtime; for example to check the given version, if it's compatible with an expected version, using the semver syntax for constraints.

Current Node.js version if read from the memory as a default value; NPM version must be retrieved by executing it, so if needed it has to be done by calling related method (could take some time).

The general behavior of check/checker methods here is to throw Error if the check does not pass (if it's false); or return true if successful, or even false

You can find Code Documentation for the API of the library here.

The package name is simplified into 'check-runtime-env', so it will be easier to get it at npm.

See the Semantic Versioning Specification at semver.

More features will be added later to the library.

Contributing

  1. Fork it ( https://github.com/smartiniOnGitHub/check-runtime-env.js/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

Licensed under Apache-2.0.