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

x-var

v2.0.1

Published

`cross-var` and `cross-env` revamped! Same syntax under Linux and Windows. `.env` supported!

Downloads

11,430

Readme

x-var

cross-var and cross-env revamped! Same syntax under Linux and Windows. .env supported!

Capabilities

Use the same syntax for all operating systems

The variable placeholders are replaced by the x-var package. The placeholders are in the form $VAR or %VAR%. See the section Caveats of the Linux and Windows command line compatibility for more details.

Look at the examples below. If you're Linux addicted:

{
  "scripts": {
    "version": "x-var echo \\$npm_package_version"
  }
}

or, if you lean towards the Windows style:

{
  "scripts": {
    "version": "x-var echo %npm_package_version%"
  }
}

Both will render the version of the package.

Source of variables

There is a bunch of sources of variables:

  • local host operating system variables
  • CI/CI
  • docker
  • user variables
  • npm config - npm configuration
  • package.json - package.json file
  • ...

All these variables are available in the npm scripts. Execute npm run env to see the list of available variables.

env files

Some projects use .env files, i.e.

  • .env - default variables
  • .env-stage - staging variables
  • .env-prod - production variables

For such cases there is an x-env utility:

{
  "scripts": {
    "run:dev": "x-env echo \\$DEPLOY_HOST",
    "run:prod": "x-env -e=.prod.env echo \\$DEPLOY_HOST"
  }
}

Note:

  • The x-env utility just loads the .env file and executes the command.
  • The -e option is used to specify the custom .env file.
  • dotenv package is used under the hood. So, almost all capabilities of these packages are kept.

Warning - the .env is loaded directly into process.env which makes all variables to be visible to the target application or script. If the .env is used by the target application, consider to use x-var to avoid conflicts.

Generally speaking, the x-env utility should be used for the most of the cases. The x-var should be used to avoid using .env files.

Caveats of the Linux and Windows command line compatibility

The Linux command line uses $ to access the environment variables, while the Windows command line uses %. However, this is not the only difference. The Linux command line replaces the variables before the command is executed, while the Windows command line replaces the variables during the command execution. Consider a simple example:

TEST=PASSED echo $TEST

The expected output is PASSED. But the actual output is empty! This is because the $TEST is replaced by the shell before the command is executed. The very same if we run the command in the npm scripts:

{
  "scripts": {
    "example": "TEST=PASSED echo $TEST"
  }
}

The output is empty as well. To fix this you might need to escape the dollar $ sign:

{
  "scripts": {
    "example": "TEST=PASSED echo \\$TEST"
  }
}

However, this won't work being run under Windows! The solution - Use the same syntax for all operating systems

Linux only solution

Despite the fact that Linux command line replaces the variables before the command is executed, there are still some useful features. If the cross-platform compatibility is not required, the encapsulation of the variable in the single quotes will work: Late evaluation of the variable:

x-var TEST=PASSED echo \$TEST

same as

TEST=PASSED x-var echo \$TEST

In this case the variable is not replaced by the shell, but by the x-var package.

Similar packages

Difference with the cross-env package

  • The cross-env package is barely maintained
  • The cross-env package is unable to load .env files. x-var uses dotenv under the hood.
  • This does not work in cross-env and it works in x-var:
    cross-env TEST=value echo \$TEST
  • And this
    cross-env TEST=value echo %TEST%
  • Cross-env relies on unmaintained cross-spawn package. The x-var uses shell.js package.

Difference with the cross-var package

  • the cross-var package in unmaintained and has dozens of vulnerabilities
  • replaced @babel with typescript and move it to devDependencies
  • fixed stderr and stdout
  • fixed exit code
  • added tests
  • added x-env utility to load .env files
  • fixed the partial replacement of the variables
  • added capability to set environment variables in the command line
  • dropped exit package
  • fixed stderr, again - by replacing the cross-spawn with the shell.js
  • added escaping of the dollar $ sign under Windows

Drawbacks

The x-var package is not a silver bullet. It has some drawbacks:

  • it uses naive but aggressive replacement of the variables
  • some things might be tricky to implement using x-var

Let me know if you have any issues or suggestions.