【发布时间】:2021-07-24 23:08:45
【问题描述】:
我正在做一个项目,每次节点运行我的代码时,浏览器控制台都会给我这个:
Uncaught TypeError: Failed to resolve module specifier "express". Relative references must start with either "/", "./", or "../".
它说这行给了我错误:
<!DOCTYPE HTML>
我不知道为什么我不断收到此错误。请帮忙。谢谢。
我已经安装了 npm 'express'、'node-fetch' 和 'esm'。
这是我的 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');
【问题讨论】:
-
你试过用 require 代替 import 语法吗?
-
是的,我有,但它给了我错误,要求未定义。
-
这样? :
const express = require("express"); -
是的,因为我使用的是 node.js
-
你检查过this 吗?
标签: javascript html node.js express