【问题标题】:I get an uncaught error after running my code运行代码后出现未捕获的错误
【发布时间】: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


【解决方案1】:

您需要做的第一件事是,安装一个捆绑器(包裹),然后您需要开发您的 package.json 脚本,该脚本从包裹中提供服务

【讨论】:

  • C:\Users\aryan_0m0ox0n\Desktop\DSS Website\node_modules\esm\esm.js:1:845:无法静态评估 fs 参数。它给了我这个错误
  • content = fs.readFileSync(file, 'ascii').对于这一行。
  • 只告诉parcel你在一个带有目标标志的节点环境中运行,例如parcel app.js --目标节点
  • 我做到了,但出现了这个错误:Cannot read property 'prototype' of undefined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
相关资源
最近更新 更多