相关疑难解决方法(0)

错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()

我正在尝试制作一个Discord机器人,它只会显示是否有人在线玩游戏。

然而我不断收到这样的消息:

[ERR_REQUIRE_ESM]:不支持 ES 模块的 require()。相反,将index.js in... 的 require 更改为动态 import(),该动态 import() 在所有 CommonJS 模块中都可用。

这是我的代码:

    module.exports = {
        name: 'username',
        description: "this is the username command",
        async execute(message, args) {

            const fetch = require('node-fetch');

            if (args.length !== 1) {
                return message.channel.send("invalid username wtf")
            }

            const ign = args[0]

            if (ign.length > 16 || ign.length < 3) {
                return message.channel.send("invalid username wtf")
            }

            const uuid = await fetch(`https://api.mojang.com/users/profiles/minecraft/${ign}`).then(data => data.json()).then(data => data.id).catch(err => message.channel.send("error wtf"));
            const onlineInfo = …
Run Code Online (Sandbox Code Playgroud)

javascript node.js node-fetch discord.js hypixel-api

251
推荐指数
3
解决办法
60万
查看次数

require('node-fetch') 给出 ERR_REQUIRE_ESM

我只是用

const fetch = require('node-fetch')
Run Code Online (Sandbox Code Playgroud)

我得到

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\Alex\Desktop\rollbot\node_modules\node-fetch\src\index.js from C:\Users\Alex\Desktop\rollbot\index.js not supported.
Instead change the require of C:\Users\Alex\Desktop\rollbot\node_modules\node-fetch\src\index.js in C:\Users\Alex\Desktop\rollbot\index.js to a 
dynamic import() which is available in all CommonJS modules.
{
  code: 'ERR_REQUIRE_ESM'
}
Run Code Online (Sandbox Code Playgroud)

我的所有其他包都可以工作,只有 node-fetch 可以做到这一点。为了使用node-fetch,我应该做什么?

javascript node.js node-fetch

82
推荐指数
5
解决办法
11万
查看次数

错误 [ERR_REQUIRE_ESM]:ES 模块的 require() ... 不支持

我正在使用 制作一个不和谐的TypeScript机器人discord.js。当我今天早上尝试编译代码时,出现以下错误:

C:\SECRET\Kostegator\dist\Util\getMeme.js:17
const node_fetch_1 = __importDefault(require("node-fetch"));
                                     ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\SECRET\Kostegator\node_modules\node-fetch\src\index.js from C:\SECRET\Kostegator\dist\Util\getMeme.js not supported.
Instead change the require of index.js in C:\SECRET\Kostegator\dist\Util\getMeme.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\Util\getMeme.js:17:38)
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\Util\index.js:15:14)
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\Commands\BotOwner\startAutoUpdate.js:4:16)
    at C:\SECRET\Kostegator\dist\Client\index.js:61:41
    at Array.forEach (<anonymous>)
    at ExtendedClient.<anonymous> (C:\SECRET\Kostegator\dist\Client\index.js:58:48)        
    at Generator.next (<anonymous>)
    at C:\SECRET\Kostegator\dist\Client\index.js:27:71
    at new Promise (<anonymous>)
    at __awaiter (C:\SECRET\Kostegator\dist\Client\index.js:23:12)
    at ExtendedClient.init (C:\SECRET\Kostegator\dist\Client\index.js:51:16)
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\index.js:19:4) {
  code: 'ERR_REQUIRE_ESM' …
Run Code Online (Sandbox Code Playgroud)

node.js typescript discord.js

34
推荐指数
3
解决办法
15万
查看次数

错误 [ERR_REQUIRE_ESM]:ES 模块的 require(),不支持 node_modules\wrap-ansi\index.js

在 Nestjs 项目中,我遇到了一个库的问题,我没有在代码中直接使用它,但它被第三方使用。

node_modules\wrap-ansi\index.js:2 中的 stringWidth = require('string-width') 不支持

这是 package.json 依赖项:

"dependencies": {
    "@nestjs-modules/mailer": "^1.9.1",
    "@nestjs/common": "^10.1.2",
    "@nestjs/config": "^3.0.0",
    "@nestjs/core": "^10.1.2",
    "@nestjs/event-emitter": "^2.0.2",
    "@nestjs/jwt": "^10.0.3",
    "@nestjs/mapped-types": "^2.0.2",
    "@nestjs/passport": "^10.0.0",
    "@nestjs/platform-express": "^10.1.2",
    "@nestjs/swagger": "^7.1.4",
    "@nestjs/throttler": "^4.0.0",
    "@nestjs/typeorm": "^10.0.0",
    "@ttshivers/automapper-classes": "^8.8.3",
    "@ttshivers/automapper-core": "^8.8.3",
    "@ttshivers/automapper-nestjs": "^8.8.3",
    "bcrypt": "^5.1.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "handlebars": "^4.7.8",
    "nodemailer": "^6.9.7",
    "passport": "^0.6.0",
    "passport-custom": "^1.1.1",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "pg": "^8.11.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.16"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.1.11",
    "@nestjs/schematics": "^10.0.1",
    "@nestjs/testing": "^10.1.2",
    "@types/bcrypt": …
Run Code Online (Sandbox Code Playgroud)

javascript node.js nestjs

10
推荐指数
2
解决办法
5203
查看次数

"type": "module" in package.json throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath)

我想在我的 nodejs 项目中使用 import 而不是使用 require。所以,我补充说,

"type": "module"
Run Code Online (Sandbox Code Playgroud)

在我的package.json 中

import index from './index.js';
Run Code Online (Sandbox Code Playgroud)

server.js 中

当我跑

节点服务器.js

错误说,

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ....
Run Code Online (Sandbox Code Playgroud)

server.conf.js粘贴在下面。

import express from 'express';

import http from 'http';
let app = express();
let server = http.createServer(app);

import morgan from 'morgan';
import methodOverride from 'method-override';;
import path from 'path';

let port = process.env.PORT || …
Run Code Online (Sandbox Code Playgroud)

import node.js package.json

6
推荐指数
2
解决办法
7724
查看次数

将图片从URL上传到Firebase存储

我想知道如何通过URL而非输入将文件上传到Firebase的存储中(例如)。我正在从网站上抓取图像并检索其URL。我想通过一个foreach语句传递这些URL,然后将它们上传到Firebase的存储中。现在,我有使用以下代码的firebase upload-via-input输入:

var auth = firebase.auth();
var storageRef = firebase.storage().ref();

function handleFileSelect(evt) {
  evt.stopPropagation();
  evt.preventDefault();
 var file = evt.target.files[0];


  var metadata = {
    'contentType': file.type
  };

  // Push to child path.
  var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);

  // Listen for errors and completion of the upload.
  // [START oncomplete]
  uploadTask.on('state_changed', null, function(error) {
    // [START onfailure]
    console.error('Upload failed:', error);
    // [END onfailure]
  }, function() {
    console.log('Uploaded',uploadTask.snapshot.totalBytes,'bytes.');
    console.log(uploadTask.snapshot.metadata);
    var url = uploadTask.snapshot.metadata.downloadURLs[0];
    console.log('File available at', url);
    // [START_EXCLUDE]
    document.getElementById('linkbox').innerHTML = …
Run Code Online (Sandbox Code Playgroud)

javascript firebase firebase-storage

5
推荐指数
3
解决办法
1万
查看次数