简体   繁体   中英

How to pass argument in command line with npm and read it in react javascript code

I run the react application using npm start which in package.json is defined as "start": "react-scripts start -o" . in the js code I have

const backendUrl = 'hardCodedUrl';

I want this property to be passed as CLI argument. like:

npm start url=newUrl

and read this in the javascript code at runtime to be able to set backendUrl. How Can I do this?

So I visited the link Andy sent and here is how I did it: Because I'm on Windows and using Terminal with PowerShell, run:

$env:REACT_APP_TT_BACKEND_URL = "Thank you Andy"
npm start

In the code

console.log('REACT_APP_TT_BACKEND_URL', process.env.REACT_APP_TT_BACKEND_URL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM