[英]How to resolve "Failed to resolve module specifier" error in a npm package?
[英]Failed to resolve module specifier "express". I am getting this error in my browser
我在我的代碼中導入了 express 但它給了我這個錯誤。
未捕獲的 TypeError:無法解析模塊說明符“express”。 相對引用必須以“/”、“./”或“../”開頭。
有人可以幫我解決這個問題。 謝謝。
這是我的 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}`));
這是我的 start.js:
// file start.js require = require('esm')(module /*, options*/); module.exports = require('./main.js');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.