{"id":22367349,"url":"https://github.com/shian15810/by-node-env","last_synced_at":"2025-07-30T17:32:53.363Z","repository":{"id":37821214,"uuid":"191374204","full_name":"shian15810/by-node-env","owner":"shian15810","description":"Run package.json scripts by NODE_ENV.","archived":false,"fork":false,"pushed_at":"2023-01-04T05:54:51.000Z","size":646,"stargazers_count":6,"open_issues_count":13,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-11T20:48:08.208Z","etag":null,"topics":["cross-per-env","development","dotenv","env","if-env","node","node-env","npm","nps","nps-utils","package-json","per-env","pnpm","production","run","run-script","scripts","test","yarn","yo-env"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/by-node-env","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shian15810.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-11T13:10:58.000Z","updated_at":"2023-10-17T14:54:24.000Z","dependencies_parsed_at":"2023-02-02T00:01:42.857Z","dependency_job_id":null,"html_url":"https://github.com/shian15810/by-node-env","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shian15810%2Fby-node-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shian15810%2Fby-node-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shian15810%2Fby-node-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shian15810%2Fby-node-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shian15810","download_url":"https://codeload.github.com/shian15810/by-node-env/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228164487,"owners_count":17879085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-per-env","development","dotenv","env","if-env","node","node-env","npm","nps","nps-utils","package-json","per-env","pnpm","production","run","run-script","scripts","test","yarn","yo-env"],"created_at":"2024-12-04T18:17:12.713Z","updated_at":"2024-12-04T18:17:13.502Z","avatar_url":"https://github.com/shian15810.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# by-node-env\n\nRun package.json scripts by NODE_ENV.\n\n[![Travis (.com)](https://img.shields.io/travis/com/shian15810/by-node-env.svg)](https://travis-ci.com/shian15810/by-node-env)\n[![npm](https://img.shields.io/npm/v/by-node-env.svg)](https://www.npmjs.com/package/by-node-env)\n[![npm](https://img.shields.io/npm/dw/by-node-env.svg)](https://npm-stat.com/charts.html?package=by-node-env)\n[![NPM](https://img.shields.io/npm/l/by-node-env.svg)](https://choosealicense.com/licenses/mit/)\n\n## Installation\n\nInstall with **npm**:\n\n```sh\nnpm install by-node-env\n```\n\nInstall with **pnpm**:\n\n```sh\npnpm install by-node-env\n```\n\nInstall with **Yarn**:\n\n```sh\nyarn add by-node-env\n```\n\n## Features\n\n-   [x] Read `NODE_ENV` from `process.env`.\n-   [x] Read `NODE_ENV` from **.env** file.\n-   [x] Defaults `NODE_ENV` to `development`.\n-   [x] Customize `process.env` for each `NODE_ENV`.\n-   [x] Clearer, concise **scripts** in **package.json**.\n-   [x] No more **Bash** scripting in **package.json**.\n-   [x] Works on **Linux**, **macOS**, and **Windows**.\n-   [x] Compatible with **npm**, **pnpm**, and **Yarn**.\n-   [x] Consistent workflow for any `NODE_ENV`:\n    1. `npm install` or `pnpm install` or `yarn install`.\n    2. `npm start` or `pnpm start` or `yarn start`.\n\n## Problem\n\nWhen developing a **Node.js** application in `development` mode, we often use these different commands: `npm run serve`, `npm run watch`, `npm run dev`, etc. In addition, we also set `NODE_ENV=development` as an environment variable.\n\nWhen deploying to `production`, we often use these different commands: `npm start`, `npm build`, `npm run prod`, etc. `NODE_ENV=production` is a must-have environment variable in this case.\n\nThe **package.json** might look like this for those situations mentioned above:\n\n```json\n{\n    \"scripts\": {\n        \"watch\": \"webpack -d --watch\",\n        \"build\": \"webpack -p\",\n\n        \"dev\": \"nodemon src\",\n        \"prod\": \"node dist\",\n\n        \"serve\": \"npm run watch \u0026 npm run dev\",\n        \"start\": \"npm build \u0026\u0026 npm run prod\"\n    }\n}\n```\n\nWorking on multiple projects with different commands can be very confusing and forgetting, especially under heavy cognitive load. As a result, we spend a lot of time consulting the **README** or the **scripts** field in **package.json**.\n\n## Solution\n\n### package.json\n\n```json\n{\n    \"scripts\": {\n        \"build\": \"by-node-env\",\n\n        \"build:development\": \"webpack -d --watch\",\n        \"build:production\": \"webpack -p\",\n\n        \"start\": \"by-node-env\",\n\n        \"start:development\": \"npm build \u0026 nodemon src\",\n        \"start:production\": \"npm build \u0026\u0026 node dist\"\n    }\n}\n```\n\n`npm build` and `npm start` have long been the de facto commands to _build_ and _start_ a **Node.js** application, respectively.\n\nBesides that, `NODE_ENV` should always be explicitly set as an environment variable for best practice. A lot of popular frameworks expect `NODE_ENV` to be set as well.\n\nWhy not combine both, so that when `NODE_ENV=production`, executing `npm start` will spawn `npm run start:production`. Similarly, when `NODE_ENV=development`, executing `npm start` will spawn `npm run start:development`.\n\nArbitrary `NODE_ENV` and **scripts** work too, refer to more examples below.\n\n## NODE_ENV\n\nThe priority order of resolving `NODE_ENV` is as follows:\n\n1. Environment variable.\n2. **.env** file in project root directory.\n3. `development` (default).\n\nThe **.env** file, if present, must be located in the root directory of your project, where **package.json** lie.\n\nThe resolved `NODE_ENV` is available as `process.env.NODE_ENV` in your application at runtime.\n\n## Examples\n\n### Example 1a\n\n#### Example 1a: package.json\n\n```jsonc\n{\n    \"scripts\": {\n        \"start\": \"by-node-env\", // 1\n\n        \"start:development\": \"ts-node src\", // 2a\n        \"start:production\": \"ts-node-dev src\" // 2b\n    }\n}\n```\n\n1. `npm start`: **1** :arrow_right: **2a**\n2. `NODE_ENV=development npm start`: **1** :arrow_right: **2a**\n3. `NODE_ENV=production npm start`: **1** :arrow_right: **2b**\n\n### Example 1b\n\n#### Example 1b: .env\n\n```ini\nNODE_ENV=production\n```\n\n#### Example 1b: package.json\n\n```jsonc\n{\n    \"scripts\": {\n        \"start\": \"by-node-env\", // 1\n\n        \"start:development\": \"ts-node src\", // 2a\n        \"start:production\": \"ts-node-dev src\" // 2b\n    }\n}\n```\n\n1. `npm start`: **1** :arrow_right: **2b**\n2. `NODE_ENV=development npm start`: **1** :arrow_right: **2a**\n3. `NODE_ENV=production npm start`: **1** :arrow_right: **2b**\n\n### Example 2\n\n#### Example 2: package.json\n\n```jsonc\n{\n    \"scripts\": {\n        // If NODE_ENV is missing, defaults to \"development\".\n        \"build\": \"by-node-env\",\n\n        \"build:development\": \"webpack -d --watch\",\n        \"build:production\": \"webpack -p\",\n        \"build:staging\": \"webpack -p\",\n\n        // Deployment will not work unless NODE_ENV=production is explicitly set.\n        \"deploy\": \"by-node-env\",\n\n        \"predeploy:production\": \"docker build -t ${DOCKER_USER}/${DOCKER_PROJECT} .\",\n        \"deploy:production\": \"docker push ${DOCKER_USER}/${DOCKER_PROJECT}\",\n\n        // \"npm start\" is _the_ command to start the server across all environments.\n        \"start\": \"by-node-env\",\n\n        \"start:development\": \"npm run build:development\",\n\n        \"prestart:production\": \"npm run build\",\n        \"start:production\": \"start-cluster build/server/server.js\",\n\n        \"prestart:staging\": \"npm run build\",\n        \"start:staging\": \"start-cluster build/server/server.js\",\n\n        // Explicitly set NODE_ENV, which is helpful in CI.\n        \"test\": \"NODE_ENV=test by-node-env\",\n\n        \"test:test\": \"mocha\"\n    }\n}\n```\n\n## Contributing\n\nEncounter bugs or having new suggestions?\n\nIssues, comments, and PRs are always welcomed!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshian15810%2Fby-node-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshian15810%2Fby-node-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshian15810%2Fby-node-env/lists"}