I've developed a custom script to store some fixed data onto my file system. The script is located at the following path:
./lib/createLinks.js
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) => {
if (err) throw err
console.info('Global data written to file')
})
}
async function main() {
try {
await createLinks()
} catch (err) {
throw new Error(err)
}
}
main()
"create-links": "node ./lib/createLinks",
"dev": "yarn create-links && next dev",
"build": "yarn create-links && next build",
Unfortunately, I am facing an issue where my environment variables are undefined when attempting to execute the script.
The variables are stored in the root directory in a file named .env.local.
What could be causing this problem?