Estoy trabajando en un proyecto y cada vez que el nodo ejecuta mi código, la consola del navegador me da esto:
Uncaught TypeError: Failed to resolve module specifier "express". Relative references must start with either "/", "./", or "../".
Dice que esta línea me da el error:
<!DOCTYPE HTML>
No sé por qué sigo recibiendo este error. Por favor ayuda. Gracias.
Y tengo npm instalado 'express', 'node-fetch' y 'esm'.
Este es mi archivo main.js:
import express from 'express'; import fetch from 'node-fetch'; const { clientID, clientSecret, port } = require('./config.json'); const app = express(); app.use(express.static('public')); app.get('/', async ({ query }, response) => { const { code } = query; if (code) { try { const oauthResult = await fetch('https://discord.com/api/oauth2/token', { method : 'POST', body : new URLSearchParams({ client_id : clientID, client_secret : clientSecret, code, grant_type : 'authorization_code', redirect_uri : `http://localhost:${port}`, scope : 'identify' }), headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } }); const oauthData = await oauthResult.json(); const userResult = await fetch('https://discord.com/api/users/@me', { headers : { authorization : `${oauthData.token_type} ${oauthData.access_token}` } }); console.log(await userResult.json()); } catch (error) { // NOTE: An unauthorized token will not throw an error; // it will return a 401 Unauthorized response in the try block above console.error(error); } } return response.sendFile('index.html', { root: '.' }); }); app.listen(port, () => console.log(`App listening at http://localhost:${port}`));
Y este es mi start.js:
// file start.js require = require('esm')(module /*, options*/); module.exports = require('./main');
lo primero que debe hacer es instalar un paquete (paquete), luego debe desarrollar su secuencia de comandos package.json que sirve desde el paquete