31

I have a code that reads port number from environment variable or from config. Code looks like this

const port = process.env.PORT || serverConfig.port; await app.listen(port); 

To run app without defining environment variable, I run following yarn command.

yarn start:dev

This command works successfully in Linux shell and Windows command line.

Now, I want to pass environment variable. I tried following,

PORT=2344 yarn start:dev

This commands works successfully in Linux shell but failing in Windows command line. I tried following ways but couldn't get it to work.

Tried: PORT=2344 yarn start:dev

I got error: 'PORT' is not recognized as an internal or external command, operable program or batch file.

Tried: yarn PORT=2344 start:dev

I got error: yarn run v1.17.3 error Command "PORT=2344" not found. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Any idea please? I know, I can define environment variables from System Properties in Windows. But any way if I can do it from command line?

1
  • It depends on the code you are running. If you are using Next.js framework for instance, using yarn dev will automatically look for a .env.development environment file and read those variables. Commented Oct 4, 2024 at 14:16

8 Answers 8

21

i'd suggest you use the NPM module called cross-env. it allows adding particular env variables on the command line regardless of platform. with that said, you may try:

$ cross-env PORT=2344 yarn start:dev 
Sign up to request clarification or add additional context in comments.

Comments

7

You can chain commands on the Windows command prompt with &(or &&). To set an environment variable you need to use the set command.

The result should look like this: set PORT=1234 && yarn start:dev.

1 Comment

Hi - beware of this syntax not working on yarn 3.x. Should use cross-env instead
4

Put .env file at root. Then following command will expose content of .env file and then run yarn start command

$ source .env && yarn start 

or this command

$ export $(cat .env) && yarn start 

If update any variable in .env then close the terminal and open new terminal window and can again run above command. Or else can also run unset command to remove existing var.

unset VAR_NAME 

1 Comment

If you use the source .env method, you may need to have add export to your variables in the .env file, e.g. export ENV_VAR=123
2

Found a solution for this problem in Windows command prompt.

  1. Create a .env file in project root folder (outside src folder).

  2. Define PORT in it. In my case, contents of .env file will be,

PORT=2344

  1. Run yarn start:dev

  2. Application will use port number that you have specified in .env file.

1 Comment

I think that works only if you use the dotenv module which is not there by default.
0

You can use popular package dotenv:

create a file .env in root directory put all your env vars

e.g.:

ENV=DEVELOPMENT 

run your code like this

$ node -r dotenv/config your_script.js 

here the explanation:

[https://github.com/motdotla/dotenv#preload]

2 Comments

How do this but with a script instead of a file? E.g. node -r dotenv/config npm start or something like that
@Nermin import dotenv/config at the top of your file.
0

To define environment variables in the Windows command prompt we can use the set command, you can then split your call into two lines.

set PORT=2344 yarn start:dev 

The set command persists within the current command prompt, so you only need to run it once.

The equivalent command in bash is 'export'.

Comments

0

FYI (not a direct answer). I was attempting this in VS Code - passing .env variables through yarn to a JavaScript app. Google had very few examples so I'm sharing this for posterity as it's somewhat related.

The following simply substitutes text normally placed directly into the package.json or script file. Use this to quickly obfuscate or externalize your delivery configurations.

In Environment Variable File (.env)

PORT=2344 

In Yarn File (package.json)

source .env; yarn ./start.sh --port $PORT 

In Yarn Script (start.sh)

#!/bin/bash while [ $? != 0 ]; do node dist/src/index.js $1; #replace with your app call# done 

The app then accepts port as a variable. Great for multi-tenant deployments.

Comments

0

In my case (Ubuntu) this has worked:

$ PORT=3001 yarn start 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.