npm config's somewhat hidden variables

Timlah's Gravatar image
Written by: Timlah | Written on: 8 Aug 2025 | Estimated reading time: 2 minutes

Recently I ran into a problem at work, where we wanted to utilise environment variables with a productionised script. Naturally, I didn't want consumers to have to go to each of their repos that will utilise our productionised script, then individually set up environment variables for the underlying Operating Systems. Instead, I just wanted to be able to set it in .npmrc and off we go.

But there were some small caveats I ran into.

Testing with Jest

When you use something like Jest for unit testing, a test might look something like this:

describe('check 1 and 1 is equal to 2', () => {
	it('should be true', () => {
		expect(1 + 1).toBe(2);
	});
});

So to make this a little bit more complicated, we'll now set a value and then expect the value to match:

describe('check value a and value b are equal', () => {
	it('should be true', () => {
		let valueA = "Test";
		let valueB = "Test";
		expect(valueA === valueB).toBe(true);
	});
});

Lovely, so now we know some super basics of what tests look like with Jest, let's move into the meat of this article.

Looking for an environment variable

Imagine the scenario where you have an application and you want to check for an environment variable. In your application, you may have something like this:

function checkForEnvironmentVariable() {
	return process.env.MEANING_OF_LIFE === '42';
}

And that would very simply just return a true or false if on your operating system, you have an environment variable called MEANING_OF_LIFE and that the value is set to 42. Cool, but if you were to have an .npmrc file, if you were to set the same variable there without it being set at the operating system level, it'll not be picked up. Why is this?

Where's my npmrc variable gone?

process.env returns environment variables set by the user. .npmrc files do not set these environment variables at the operating system level, however it does inject some variables for you to access:

describe('when MEANING_OF_LIFE is set', () => {
	it('should equal 42', () => {
		expect(process.env.MEANING_OF_LIFE === '42').toBe(true);
	});
});

The above will work at the Operating System level.

describe('when MEANING_OF_LIFE is set', () => {
	it('should equal 42', () => {
		expect(process.env.npm_config_meaning_of_life === '42').toBe(true);
	});
});

The above will work when you set the environment variable at an .npmrc level.

In conclusion

You may be able to do some interesting testing around dynamically creating an npmrc file, ensuring the application is running and then accessing the process.env.{npm_config_} variable. In the end, we figured it wasn't worth that hassle, when we found that under the hood, node will set these config variables up for you, without you having to do anything else.

Thanks for joining me for something a little different and technical today. Hopefully you got some ideas on how to use .npmrc for your own node applications going forward.

Much love and happy tinkering - Timlah