title: NextJS environment variables undefined?
body: <p>I've made a script to save some static data to my filesystem which looks like this</p>
  <p>./lib/createLinks.js</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const contentful = require('contentful')
  const fs = require('fs')
  
  require('dotenv').config({
    path: `.env.${process.env.NODE_ENV}`,
  })
  
  async function createLinks() {
    const client = contentful.createClient({
      space: process.env.NEXT_CONTENTFUL_SPACE_ID,
      accessToken: process.env.NEXT_CONTENTFUL_ACCESS_TOKEN,
    })
  
    const data = await client.getEntries({
      content_type: 'news',
    })
  
    fs.writeFile('./data/links.json', JSON.stringify(data), (err) =&gt; {
      if (err) throw err
      console.info('Global data written to file')
    })
  }
  
  async function main() {
    try {
      await createLinks()
    } catch (err) {
      throw new Error(err)
    }
  }
  
  main()</code></pre>
  </div>
  </div>
  </p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>"create-links": "node ./lib/createLinks",
      "dev": "yarn create-links &amp;&amp; next dev",
      "build": "yarn create-links &amp;&amp; next build",</code></pre>
  </div>
  </div>
  </p>
  <p>However I can't access my environment variables which is undefined when I try to run this.</p>
  <p>My variables are placed in the root in .env.local</p>
  <p>Why is this happening?</p>