标签: node.js-got

必须使用 import 加载 ES 模块: ...\node_modules\got\dist\source\index.js

还有几十个其他问题的标题基本相同,但没有一个答案似乎是相关的,只会增加混乱。

这是我的tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["es2017", "es7", "es6", "dom"],
    "declaration": true,
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是我输入的内容:

import md5 from 'js-md5';
import got from 'got';
import { Design } from './Design';
...
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

错误 [ERR_REQUIRE_ESM]:必须使用 import 加载 ES 模块:C:\Users\...\node_modules\got\dist\source\index.js 不支持 ES 模块的 require() 。

什么。我不是require,我是在用import。我在任何其他模块上都没有遇到此错误,那么为什么 Got 有所不同(并且为什么没有什么是简单的)?

为什么会发生这种情况以及如何解决它?


根据@jsejcksn 的回答,我尝试将其更改tsconfig.json为以下内容:

{
  "compilerOptions": {
    "target": "es6", …
Run Code Online (Sandbox Code Playgroud)

node.js typescript node.js-got

12
推荐指数
2
解决办法
4万
查看次数

获取 npm 模块:如何将不记名令牌添加到 POST 请求

我最近发现request不再维护,所以我找到的最好的替代方案是got。我正在尝试对 REST 服务器进行外部 API 调用,但我不确定如何在 POST 请求的授权标头中添加不记名令牌。

这是我的代码:

const response = await got.post(
  "https://${SERVER}/${SOME_ID}/conversations/${CONVERSATION_ID}/messages",
  {
    json: [
      {
        text: req.body.message,
        type: "SystemMessage",
      }
    ],
    responseType: "json",
    headers: {
        token: "Bearer pXw4BpO95OOsZiDQS7mQvOjs"
    }
  }
);
Run Code Online (Sandbox Code Playgroud)

这会产生一个401 Unauthorized. 我无法在 GOT 提供的文档中找到此类实现的方向。由于关于这个包的查询并不多,所以我在谷歌上也没有成功找到任何东西。如果有人可以在这方面帮助我,那将非常有帮助!

node.js npm node.js-got

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

如何导入打字稿?

我正在尝试在我的打字稿程序中导入从“got”获取的内容,以将网址转换为可流式文件,但我尝试以不同的方式导入它,并且我总是收到如下错误: can only be default-imported using the 'esModuleInterop' flag

我的问题是如何正确导入而不会出现错误?或者是否有另一种方法可以将 url 转换为打字稿中的可流式文件?

import { sha256 } from 'js-sha256';
import {Metadata} from './metadata';
import * as fs from 'fs'
const pinataSDK = require('@pinata/sdk');
import got from 'got';
export const ARC3_NAME_SUFFIX = "@arc3"
export const ARC3_URL_SUFFIX = "#arc3"
export const METADATA_FILE = "metadata.json"
export const JSON_TYPE = 'application/json'
export const JSONObject = "metadata.json"
export function asaURL(cid: string): string { return ipfsURL(cid)+ARC3_URL_SUFFIX }
export function ipfsURL(cid: string): string { return "ipfs://"+cid }
Run Code Online (Sandbox Code Playgroud)

typescript node.js-got

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

如何从 CommonJS 模块导入 ESM Javascript 文件?得到。错误:不支持

我的项目完全是用 CommonJS 模块编写的,我无法更改它。我正在尝试使用一个 ESM 库。该库是 Got 库(https://github.com/sindresorhus/got)。

这是我的代码

const request = require('request');
const Promise = require('bluebird');
// require('./wrapped-got');
var wrappedgot = null;

function HTTPRequestV2(hostURL, defaultOptions) {
    this.hostUrl = hostURL;
    this.requestWrapper = request.defaults(defaultOptions);
}


HTTPRequestV2.prototype.init = async function(){
    wrappedgot = await import('got');
    /*return import('got')
        .then(({default: theDefault}) => {
            wrappedgot= theDefault;
            console.log(theDefault);

        });
    console.log(wrappedgot);*/
};
Run Code Online (Sandbox Code Playgroud)

但在运行此命令时,我收到错误Not Supported on thewrappedgot = wait import('got'); 线

我尝试按照问题页面中的建议使用动态导入函数的解决方法,但由于上述错误而失败 https://github.com/sindresorhus/got/issues/1789

甚至尝试运行他们的示例代码,但也失败并出现相同的错误 https://gist.github.com/szmarczak/0f2b70b2a1ed52a56a6d391ac02d094d

- - - 更新 - - - -

我使用的是 Node 版本 12.14.1,它支持异步和等待。我读过 …

javascript commonjs node.js es6-modules node.js-got

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

尝试导入 create-react-app 中的 npm 时出错

我正在尝试在 create-react-app 应用程序中导入 got ( https://www.npmjs.com/package/got ),当我只是尝试将其导入到一个文件中时,甚至不尝试执行任何代码应用程序无法编译,我收到以下错误:

util.js:601 Uncaught TypeError: The "original" argument must be of type Function
    at Object.promisify (util.js:601)
    at Object.<anonymous> (get-body-size.js:7)
    at Object../node_modules/got/dist/source/utils/get-body-size.js (get-body-size.js:30)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Object.<anonymous> (normalize-arguments.js:15)
    at Object../node_modules/got/dist/source/normalize-arguments.js (normalize-arguments.js:441)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Object.<anonymous> (as-promise.js:8)
    at Object../node_modules/got/dist/source/as-promise.js (as-promise.js:152)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Object../node_modules/got/dist/source/create.js (create.js:4)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Object../node_modules/got/dist/source/index.js (index.js:7)
    at __webpack_require__ (bootstrap:784)
    at fn (bootstrap:150)
    at Object../src/utils/ph_loader.js (ph_loader.js:1)
    at __webpack_require__ …
Run Code Online (Sandbox Code Playgroud)

javascript npm create-react-app node.js-got

1
推荐指数
1
解决办法
1520
查看次数