Bas*_*ord 162 javascript node.js nft
尝试使用 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
Run Code Online (Sandbox Code Playgroud)
我的理解是他们已经更新了节点文件,所以我需要与教程中不同的代码,但我不知道我应该更改哪一个,在哪里以及更改什么。请尽可能具体
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));
})
Run Code Online (Sandbox Code Playgroud)
ABD*_*NOV 161
是因为node-fetch
包的原因。由于此软件包的最新版本仅支持 ESM,因此您必须将其降级到旧版本node-fetch@2.6.1
或更低版本。
npm i node-fetch@2.6.1
这应该可以解决问题。
小智 40
无需使用旧版本。您可以使用此行代替“require”
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
Run Code Online (Sandbox Code Playgroud)
小智 12
转到您的 package.json 文件并写入:
"type": "module",
Run Code Online (Sandbox Code Playgroud)
以上调试。
并且不要写入require('chalk')
.js 文件,而是将其更改为import chalk from 'chalk'
小智 11
当您安装的最新版本的软件包存在模块导入问题时,可能会发生这种情况。
就我而言,安装最新版本的软件包后出现错误 crypto-random-string
。要了解哪个包导致此错误,请检查报告的上述错误之前的消息。就我而言,它是这样写的:
error: uncaughtException: require() of ES Module /Users/myname/Documents/mydir/anotherdir/my-project/node_modules/crypto-random-string/index.js
为了解决这个问题,我只通过执行以下操作降级到早期版本:
yarn remove crypto-random-string
yarn add crypto-random-string@3.3.1
归档时间: |
|
查看次数: |
225661 次 |
最近记录: |