title: Set environment variable through Typescript script
body: <p>I want to create a hash on build and set is as environment variable. It should be accessible by node.</p>
  <p>Firstly I wrote a bash script, exported the environment variable in the script and sourced it in the <code>package.json</code>.
  Problem is node doesn't know the <code>source</code> command.
  Now I rewrote the script in Typescript (due to the whole project using TS not JS).
  In the script I set the variable as follows:</p>
  <pre class="lang-js prettyprint-override"><code>process.env.VARIABLE = hashFunction(path);
  </code></pre>
  <p>The function is called through a script in <code>package.json</code></p>
  <pre class="lang-json prettyprint-override"><code>&quot;hash&quot;: &quot;ts-node path/to/script.ts&quot;
  </code></pre>
  <p>The function works as it should, but the environment variable is not set. Can someone help me to resolve this? Is it possible to return the string outside of the script and set it from there?</p>
  <p>If possible i'd like to not use an external package.</p>
  <p>Thank you :)</p>
  <h1>Update:</h1>
  <p>I used a bash script, but with a typescript script it'd work the same way. For bash the <code>console.log</code> is replaced with <code>echo</code>.</p>
  <p><code>script.ts</code></p>
  <pre class="lang-js prettyprint-override"><code>console.log(&quot;2301293232&quot;) // The hash created by the script
  </code></pre>
  <p><code>package.json</code></p>
  <pre class="lang-json prettyprint-override"><code>&quot;scripts&quot;: {
      &quot;build&quot;: &quot;yarn run hash react-scripts build&quot;, // omit &amp;&amp;
      &quot;hash&quot;: &quot;ENV_VAR=$(ts-node script.ts)&quot;
  }
  </code></pre>
  <p>So I did the following:
  The script returns the checksum to the console/standard output. But I'll capture it before and set the printed value as environment variable in the <code>package.json</code> file. This will work as long as its the same process which starts the build.
  That is why neither</p>
  <pre class="lang-json prettyprint-override"><code>&quot;scripts&quot;: {
      &quot;build&quot;: &quot;yarn run hash &amp;&amp; react-scripts build&quot;
  }
  </code></pre>
  <p>nor</p>
  <pre class="lang-json prettyprint-override"><code>&quot;scripts&quot;: {
      &quot;build&quot;: &quot;react-scripts build&quot;,
      &quot;prebuild&quot;: &quot;ENV_VAR=$(ts-node script.ts)&quot;
  }
  </code></pre>
  <p>will work. In both examples a new process will be started and the environment variable will be lost.</p>