In my Vanilla JS project, I want use an external service file to fetch data from an API. When trying to log the data from the API call, I get the error Uncaught Error: Cannot find module ’26sb0′
The files index.html
, index.js
and apiService.js
are in the **src **folder.
I imported the javascript files in the index.html document:
<script type="module" src="index.js" defer></script>
<script type="module" src="apiService.js" defer></script>
I make the API call in the apiService.js file:
const apiUrl = 'https://api.spoonacular.com';
export const fetchData = async(path) => {
const res = await fetch(`${apiUrl}/${path}`);
const data = await res.json();
return data;
};
Finally, I import the fetchData function in my index.js file and make an API call:
import { fetchData } from './apiService';
const testData = await fetchData(`recipes/random?apiKey=xxxxxxxxxxxxxxxxxx`);
console.log(testData);