{"id":13125,"library":"env-test","title":"Mocha NODE_ENV Test Setter","description":"The `env-test` package is a small, specialized utility designed to set the `NODE_ENV` environment variable to `'test'` specifically when running Mocha tests. Its primary use case is through Mocha's `--require` flag, often configured in `mocha.opts`, to ensure that test environments consistently have `process.env.NODE_ENV = 'test'` without needing to add this line to every test file or rely on shell commands that might be skipped. Released as `1.0.0`, this package has seen no updates in over nine years, indicating it is an abandoned project. While it still functions for its narrow purpose, modern Node.js projects typically utilize more comprehensive environment management solutions like `dotenv`, `cross-env`, or `env-cmd` for loading `.env` files and conditionally setting environment variables across different platforms and contexts.","status":"abandoned","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lmtm/node-env-test","tags":["javascript","mocha","env","test"],"install":[{"cmd":"npm install env-test","lang":"bash","label":"npm"},{"cmd":"yarn add env-test","lang":"bash","label":"yarn"},{"cmd":"pnpm add env-test","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is intended to be required by a test runner like Mocha for its side-effects (setting process.env.NODE_ENV). It does not export any symbols for direct import into JavaScript/TypeScript files. Attempting to use an ES module 'import' will generally not work as intended for this package's core function as a runtime hook.","wrong":"import 'env-test';","symbol":"Side-effect require","correct":"mocha --require env-test"},{"note":"The package's function is purely side-effect based (modifying process.env). There are no exported functions, classes, or objects to be imported or required programmatically. Its utility is in being loaded by `--require`.","wrong":"const envTest = require('env-test');","symbol":"No direct import/require for symbols","correct":"/* no direct code import needed, used via CLI */"}],"quickstart":{"code":"{\n  \"name\": \"my-project\",\n  \"devDependencies\": {\n    \"mocha\": \"^10.0.0\",\n    \"env-test\": \"^1.0.0\"\n  },\n  \"scripts\": {\n    \"test\": \"mocha\"\n  }\n}\n\n// 1. Install the package\n// npm install --save-dev env-test mocha\n\n// 2. Create or edit test/mocha.opts\n//    --require env-test\n//    --recursive\n//    --timeout 5000\n//    --ui bdd\n\n// 3. Create a test file (e.g., test/example.test.js)\nconst assert = require('assert');\n\ndescribe('Environment Test', function() {\n  it('should have NODE_ENV set to \\'test\\'', function() {\n    assert.strictEqual(process.env.NODE_ENV, 'test', 'NODE_ENV should be \\'test\\'.');\n  });\n\n  it('should demonstrate a basic test', function() {\n    const a = 1;\n    const b = 2;\n    assert.strictEqual(a + b, 3, '1 + 2 should equal 3');\n  });\n});\n\n// 4. Run tests from your terminal: npm test\n//    NODE_ENV will be automatically set to 'test' by env-test before tests run.\n","lang":"javascript","description":"This quickstart demonstrates how to set up `env-test` with Mocha using `mocha.opts` to automatically ensure `NODE_ENV` is set to 'test' for your test runs."},"warnings":[{"fix":"Migrate to `cross-env` for setting environment variables in `package.json` scripts (`cross-env NODE_ENV=test mocha`) or `dotenv` for loading `.env` files in your test setup if more complex environment management is needed.","message":"The `env-test` package is abandoned and has not been updated in over nine years. While it may still function, it is not actively maintained and may not be compatible with future Node.js versions or complex modern build pipelines. Consider more actively maintained alternatives.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"For more flexible environment variable management (e.g., loading from `.env` files, setting multiple variables, or cross-platform compatibility), use packages like `dotenv` (programmatically) or `env-cmd` / `cross-env` (via npm scripts).","message":"This package specifically sets `NODE_ENV` to 'test'. If you need other environment variables or more dynamic environment configuration, `env-test` is insufficient. Its scope is very narrow.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure that `env-test` is the sole or primary source for setting `NODE_ENV` to 'test' in your testing workflow. Review your `package.json` scripts and other test setup files for conflicting environment variable assignments.","message":"Using `--require env-test` sets `NODE_ENV` early in the process lifecycle. If other scripts or configuration files attempt to set `NODE_ENV` later or unconditionally, it might lead to unexpected overwrites or conflicts.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `env-test` is installed as a dev dependency: `npm install --save-dev env-test`.","cause":"`env-test` is not installed or not found in `node_modules`.","error":"Error: Cannot find module 'env-test'"},{"fix":"Verify that your `mocha.opts` file is in the correct location (`test/mocha.opts` by default, or specified with `--opts <path>`). Double-check the `--require env-test` line for typos. Add `console.log(process.env.NODE_ENV);` to your test setup to debug when `NODE_ENV` is being set.","cause":"The `mocha.opts` file is not being loaded, or the `--require` flag is incorrectly specified, or another process is overwriting `NODE_ENV`.","error":"mocha --require env-test` does not set NODE_ENV"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}