I have a simple example :
import { fetch } from 'node-fetch';
import {express} from 'express';
const app = express();
const PORT = 3000
const RANDOM_DOGS_ENDPOINT = 'https://dog.ceo/api/breeds/image/random';
app.get('/get_dog', async (req, res) => {
const result = await fetch(RANDOM_DOGS_ENDPOINT);
res.json(result)
})
app.listen(PORT, () => {
console.log(`Server running on port ${PORT} ..`)
});
My objetive is creating a server to provide a free endpoint : https://dog.ceo/api/breeds/image/random . Then, on my front call my own endpoint which is calling this one.
Im trying to use fetch
module to http request , I found this package (node-fetch
) to apply that. But Im facing problems about how to import package I have installed on my node_modules.
Welf, I usually to import like this :
const express = require('express')
...
And I love it, but on this case, to use fetch I have watched that i need to use this other way to import, first problem after that was i had imports by two ways : import {foo} from bar
and const express = require..
. So I found by SO that is a problem mixing this two ways.. Ok then all imports now will be by import {foo} from bar
then Im facing problems again, my currently problem is :
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47
[nodemon] app crashed - waiting for file changes before starting...
Then... What is the point if I cant import as 'import' and either 'require..'