kandi background
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Tutorial open source software, libraries, packages, source code, cloud functions and APIs.

Explore Related Topics

Popular New Releases in Tutorial

新书出版

v1.22.1

v5.1.0

v0.1.4

1.7.0

leetcode

新书出版

wtfjs

v1.22.1

intro.js

v5.1.0

algorithms

v0.1.4

LeetCode-Go

1.7.0

Popular Libraries in Tutorial

Trending New libraries in Tutorial

Top Authors in Tutorial

1

126 Libraries

4879

2

90 Libraries

1933

3

53 Libraries

23678

4

48 Libraries

67554

5

35 Libraries

2030

6

35 Libraries

463

7

33 Libraries

12103

8

33 Libraries

25961

9

30 Libraries

594

10

29 Libraries

4333

1

126 Libraries

4879

2

90 Libraries

1933

3

53 Libraries

23678

4

48 Libraries

67554

5

35 Libraries

2030

6

35 Libraries

463

7

33 Libraries

12103

8

33 Libraries

25961

9

30 Libraries

594

10

29 Libraries

4333

Trending Kits in Tutorial

Trending Discussions on Tutorial

    Instead change the require of index.js, to a dynamic import() which is available in all CommonJS modules
    Invalid CSS value error while Customizing Bootstrap 5 colors with sass 3
    How do I resolve error message: "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option"
    Why does this Cairo program put powers of 2 in the memory?
    Android sdkmanager not found. Update to the latest Android SDK and ensure that the cmdline-tools are installed to resolve this
    TypeError: load() missing 1 required positional argument: 'Loader' in Google Colab
    How can I configure Hardhat to work with RSK regtest blockchain?
    Flutter (2.5) - A splash screen was provided to Flutter, but this is deprecated
    What to use instead of TaskException for ReviewManager Api?
    FirebaseOptions cannot be null when creating the default app

QUESTION

Instead change the require of index.js, to a dynamic import() which is available in all CommonJS modules

Asked 2022-Apr-05 at 06:25

Trying to work with node/javascript/nfts, I am a noob and followed along a tutorial, but I get this error:

1error [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
2

My understanding is that they've updated the node file, so i need a different code than that in the tutorial, but i don't know which one I'm supposed to change, where and to what. Please be as specific as you can

1error [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
2const FormData = require('form-data');
3const fetch = require('node-fetch');
4const path = require("path")
5const basePath = process.cwd();
6const fs = require("fs");
7
8fs.readdirSync(`${basePath}/build/images`).foreach(file).forEach(file => {
9    const formData = new FormData();
10    const fileStream = fs.createReadStream(`${basePath}/build/images/${file}`);
11    formData.append('file',fileStream);
12
13    let url = 'https://api.nftport.xyz/v0/files';
14
15    let options = {
16      method: 'POST',
17      headers: {
18        Authorization: '[...]',
19      },
20      body: formData
21    };
22    
23    fetch(url, options)
24      .then(res => res.json())
25      .then(json => {
26       const fileName = path.parse(json.file_name).name;
27       let rawdata = fs.readFileSync(`${basePath}/build/json/${fileName}.json`);
28       let metaData = JSON.parse(rawdata);
29
30       metaData.file_url = json.ipfs_url;
31
32       fs.writeFileSync(`${basePath}/build/json${fileName}.json`, JSON.stringify(metaData, null, 2));
33
34       console.log(`${json.file_name} uploaded & ${fileName}.json updated!`);
35      })
36      .catch(err => console.error('error:' + err));
37})
38
39

ANSWER

Answered 2021-Dec-31 at 10:07

It is because of the node-fetch package. As recent versions of this package only support ESM, you have to downgrade it to an older version node-fetch@2.6.1 or lower.

npm i node-fetch@2.6.1

This should solve the issue.

Source https://stackoverflow.com/questions/70541068

Community Discussions contain sources that include Stack Exchange Network

    Instead change the require of index.js, to a dynamic import() which is available in all CommonJS modules
    Invalid CSS value error while Customizing Bootstrap 5 colors with sass 3
    How do I resolve error message: "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option"
    Why does this Cairo program put powers of 2 in the memory?
    Android sdkmanager not found. Update to the latest Android SDK and ensure that the cmdline-tools are installed to resolve this
    TypeError: load() missing 1 required positional argument: 'Loader' in Google Colab
    How can I configure Hardhat to work with RSK regtest blockchain?
    Flutter (2.5) - A splash screen was provided to Flutter, but this is deprecated
    What to use instead of TaskException for ReviewManager Api?
    FirebaseOptions cannot be null when creating the default app

QUESTION

Instead change the require of index.js, to a dynamic import() which is available in all CommonJS modules

Asked 2022-Apr-05 at 06:25

Trying to work with node/javascript/nfts, I am a noob and followed along a tutorial, but I get this error:

1error [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
2

My understanding is that they've updated the node file, so i need a different code than that in the tutorial, but i don't know which one I'm supposed to change, where and to what. Please be as specific as you can

1error [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
2const FormData = require('form-data');
3const fetch = require('node-fetch');
4const path = require("path")
5const basePath = process.cwd();
6const fs = require("fs");
7
8fs.readdirSync(`${basePath}/build/images`).foreach(file).forEach(file => {
9    const formData = new FormData();
10    const fileStream = fs.createReadStream(`${basePath}/build/images/${file}`);
11    formData.append('file',fileStream);
12
13    let url = 'https://api.nftport.xyz/v0/files';
14
15    let options = {
16      method: 'POST',
17      headers: {
18        Authorization: '[...]',
19      },
20      body: formData
21    };
22    
23    fetch(url, options)
24      .then(res => res.json())
25      .then(json => {
26       const fileName = path.parse(json.file_name).name;
27       let rawdata = fs.readFileSync(`${basePath}/build/json/${fileName}.json`);
28       let metaData = JSON.parse(rawdata);
29
30       metaData.file_url = json.ipfs_url;
31
32       fs.writeFileSync(`${basePath}/build/json${fileName}.json`, JSON.stringify(metaData, null, 2));
33
34       console.log(`${json.file_name} uploaded & ${fileName}.json updated!`);
35      })
36      .catch(err => console.error('error:' + err));
37})
38
39

ANSWER

Answered 2021-Dec-31 at 10:07

It is because of the node-fetch package. As recent versions of this package only support ESM, you have to downgrade it to an older version node-fetch@2.6.1 or lower.

npm i node-fetch@2.6.1

This should solve the issue.

Source https://stackoverflow.com/questions/70541068