尝试使用 node/javascript/nfts,我是一个菜鸟,按照教程进行操作,但出现此错误:
error [ERR_REQUIRE_ESM]: require() of ES Module [...] is not supported. Instead change the require of index.js [ in my file...] to a dynamic import() which is available in all CommonJS modules
我的理解是他们已经更新了节点文件,所以我需要与教程中的代码不同的代码,但我不知道我应该更改哪个代码、更改位置和内容。请尽可能具体
const FormData = require('form-data');
const fetch = require('node-fetch');
const path = require("path")
const basePath = process.cwd();
const fs = require("fs");
fs.readdirSync(`${basePath}/build/images`).foreach(file).forEach(file => {
const formData = new FormData();
const fileStream = fs.createReadStream(`${basePath}/build/images/${file}`);
formData.append('file',fileStream);
let url = 'https://api.nftport.xyz/v0/files';
let options = {
method: 'POST',
headers: {
Authorization: '[...]',
},
body: formData
};
fetch(url, options)
.then(res => res.json())
.then(json => {
const fileName = path.parse(json.file_name).name;
let rawdata = fs.readFileSync(`${basePath}/build/json/${fileName}.json`);
let metaData = JSON.parse(rawdata);
metaData.file_url = json.ipfs_url;
fs.writeFileSync(`${basePath}/build/json${fileName}.json`, JSON.stringify(metaData, null, 2));
console.log(`${json.file_name} uploaded & ${fileName}.json updated!`);
})
.catch(err => console.error('error:' + err));
})
原文由 Basse Nord 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是因为
node-fetch
包。由于此软件包的最新版本仅支持 ESM,因此您必须将其降级到旧版本node-fetch@2.6.1
或更低版本。npm i node-fetch@2.6.1
这应该可以解决问题。