セキュリティ カメラのストリームを分析するプログラムを作成していますが、最初の行で行き詰まってしまいました。現時点では、.js ファイルには node-fetch のインポートしかなく、エラー メッセージが表示されます。何が間違っているのでしょうか?
Windows Subsystem for Linux で Ubuntu 20.04.2 LTS を実行しています。
ノードバージョン:
user@MYLLYTIN:~/CAMSERVER$ node -v
v14.17.6
node-fetch パッケージのバージョン:
user@MYLLYTIN:~/CAMSERVER$ npm v node-fetch
[email protected] | MIT | deps: 2 | versions: 63
A light-weight module that brings Fetch API to node.js
https://github.com/node-fetch/node-fetch
keywords: fetch, http, promise, request, curl, wget, xhr, whatwg
dist
.tarball: https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz
.shasum: 79da7146a520036f2c5f644e4a26095f17e411ea
.integrity: sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==
.unpackedSize: 75.9 kB
dependencies:
data-uri-to-buffer: ^3.0.1 fetch-blob: ^3.1.2
maintainers:
- endless <[email protected]>
- bitinn <[email protected]>
- timothygu <[email protected]>
- akepinski <[email protected]>
dist-tags:
latest: 3.0.0 next: 3.0.0-beta.10
published 3 days ago by endless <[email protected]>
esm パッケージ バージョン:
user@MYLLYTIN:~/CAMSERVER$ npm v esm
[email protected] | MIT | deps: none | versions: 140
Tomorrow's ECMAScript modules today!
https://github.com/standard-things/esm#readme
keywords: commonjs, ecmascript, export, import, modules, node, require
dist
.tarball: https://registry.npmjs.org/esm/-/esm-3.2.25.tgz
.shasum: 342c18c29d56157688ba5ce31f8431fbb795cc10
.integrity: sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==
.unpackedSize: 308.6 kB
maintainers:
- jdalton <[email protected]>
dist-tags:
latest: 3.2.25
published over a year ago by jdalton <[email protected]>
.js ファイルの内容 (文字通りインポート以外のものはありません):
user@MYLLYTIN:~/CAMSERVER$ cat server.js
import fetch from "node-fetch";
結果:
user@MYLLYTIN:~/CAMSERVER$ node -r esm server.js
/home/user/CAMSERVER/node_modules/node-fetch/src/index.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/user/CAMSERVER/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /home/user/CAMSERVER/node_modules/node-fetch/src/index.js from /home/user/CAMSERVER/server.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/user/CAMSERVER/node_modules/node-fetch/package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13) {
code: 'ERR_REQUIRE_ESM'
}
user@MYLLYTIN:~/CAMSERVER$
ベストアンサー1
node-fetchはバージョン1.0でESM専用パッケージに変更されました。3.0.0-ベータ.10. node-fetch は ESM 専用のモジュールです。require を使用してインポートすることはできません。
あるいは、CommonJS の async import() 関数を使用して、node-fetch を非同期にロードすることもできます。
// mod.cjs
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
またはそのままv2彼らが言うようにREADME
node-fetch
ESM 専用のモジュールです。require でインポートすることはできません。ESM を自分で使用しない限り、CommonJS で構築された v2 のままにしておくことをお勧めします。引き続き、重要なバグ修正を公開していきます。
編集
ドキュメントでそれを見て、使用したところ、スクリプトがクラッシュしました:
エラー TS2556: スプレッド引数はタプル型であるか、残りのパラメータに渡される必要があります。
Typescriptを使用している場合は、次のようにする必要があります
import { RequestInfo, RequestInit } from "node-fetch";
const fetch = (url: RequestInfo, init?: RequestInit) => import("node-fetch").then(({ default: fetch }) => fetch(url, init));