0

I'm attempting to build a environment variable: PUBLIC_URL for a react app out of two pre-existing ENV vars DOMAIN and URL_PREFIX. I'm hoping to do so right before running the 'react-scripts build' command within an npm script.

I'm using a .env-cmdrc.json file for storing multiple environments variables all in one place. I'm currently building the var and calling the command like so.

env-cmd -e shared,staging --use-shell "export PUBLIC_URL=https://$DOMAIN/$URL_PREFIX && react-scripts build"

I'm running the command from the 'client' folder which contains both .env-cmdrc.json and package.json. It results in the following error indicating the variables were never expanded.

  throw new ERR_INVALID_URL(input);
  ^

TypeError [ERR_INVALID_URL]: Invalid URL: https:///
    at onParseError (internal/url.js:259:9)
    at new URL (internal/url.js:335:5)
    at getPublicUrlOrPath (/home/nathanielw/.nvm/versions/node/v14.16.0/lib/node_modules/react-scripts/node_modules/react-dev-utils/getPublicUrlOrPath.js:36:28)
    at Object.<anonymous> (/home/nathanielw/.nvm/versions/node/v14.16.0/lib/node_modules/react-scripts/config/paths.js:26:25)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18) {
  input: 'https:///',
  code: 'ERR_INVALID_URL'
}

the variables $DOMAIN and $URL_PREFIX are consumed just fine by the react-scripts build command: env-cmd -e shared,staging react-scripts build or just printenv DOMAIN , but they do not appear in other commands such as printing out in echo $DOMAIN

I think I'm missing something when it comes to passing environments between commands but I can't figure out why env-cmd -e shared,staging printenv DOMAIN would work, but env-cmd -e shared,staging echo $DOMAIN wouldn't.

Nate W
  • 25
  • 1
  • 6

1 Answers1

0

strange behavior of 'echo' in 'env' bash

Found a relevant question. I just needed to switch to single quotes instead of double to prevent the $DOMAIN from being evaluated before the env-cmd is run and sets those variables. Everything else was working great.

Nate W
  • 25
  • 1
  • 6
  • If this is solved in another question, you should just flag it as a duplicate, not post an answer containing a link. – Barmar Apr 23 '21 at 19:22