I am creating an NPM package using React (basically is a package that performs queries to an API and displays the response in a page)
Since I have different apps that are going to use the package, I need to call different API urls. I thought that I can leave the API url as a placeholder (process.env.REACT_APP_API_URL) in the package using .env and then pass the url in the .env file in the consumer application. However, even thought the application has an .env file and the url values are there, the API call is being done to an 'undefined' url.
My thoughts are that when the package is built, instead of having the placeholder for my variable, the placeholder is replaced for 'undefined' and there is my problem.
This makes me think the I am taking the wrong approach and I need to customize this value in another manner. Has anyone faced something similar in the past and has a good solution?
This is how this looks like in my package:
import axios from 'axios';
const apiClient = axios.create({
baseURL: `${process.env.REACT_APP_API_URL}/`,
headers: {
"Accept": "application/json",
"Content-Type": "application/json; charset=utf-8",
},
});
export default apiClient;
This is my .env file in my React App:
REACT_APP_API_URL="http://localhost:5001/"
Thanks