I have a react project setup and I want to run some node script on build time to fetch JSON data from external API and save it as a static resource on server and then use that file in the app.
This is the brief overview of the project structure:
src
|-dir
| |-helpers.ts
|
|-node-script.js
the node script I am trying to run
import fetch from "node-fetch"
import fs from "fs";
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then((response) => response.json())
.then((json) => {
fs.writeFile('./src/data.json', JSON.stringify(json), (err) => {
if (err) {
throw new Error('Something went wrong writing the file.')
}
console.log('JSON written to file. Contents:', JSON.stringify(json));
})
})
This script doesn't run and gives me this error.
(node:62124) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension
I know adding type:module
in my package.json resolves this but I don't want to do that as I am afraid it will going to mess with other dependencies in the frontend project.
I am not able to find an alternative solution and need some help here.