The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

Viewed 7488

I do according to the lesson from https://github.com/UniqueNetwork/nft-workshop step 5 I'm writing a command "node nft-generator.js" this error appears

Possible combinations: 405
node:internal/fs/utils:670
    throw new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined
    at Object.mkdirSync (node:fs:1329:10)
    at generateNFTs (C:\Users\wasab\Desktop\nft\nft-workshop-master\nft-generator.js:64:8)
    at main (C:\Users\wasab\Desktop\nft\nft-workshop-master\nft-generator.js:112:3)
    at Object.<anonymous> (C:\Users\wasab\Desktop\nft\nft-workshop-master\nft-generator.js:116:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'

file "nft-generator.js"

const fs = require("fs");
var BigNumber = require('bignumber.js');
const config = require('./config');
const attributes = require('./attributes');

function getRandomInt(max) {
  return BigNumber.random(20).multipliedBy(max).integerValue();
}

function codeToArray(code) {
  let arr = [];

  for (let i=attributes.length-1; i>=0; i--) {

    // property gets the actual value of property i...
    let property = parseInt(code.mod(attributes[i].count).toFixed());

    // If this is a required property, the actual value ranges from 1 to attributes[i].count, so that required properties are never 0
    if (attributes[i].required) {
      arr[i] = property+1;
    }
    // If this is an optional property, the actual value ranges from 0 to attributes[i].count. 0 means this trait is not present
    else {
      arr[i] = property;
    }
    code = code.minus(arr[i]).dividedBy(attributes[i].count).integerValue();
  }

  return arr;
}

function bnIncludes(arr, bn) {
  for (let i=0; i<arr.length; i++) {
    if (arr[i].eq(bn)) return true;
  }
  return false;
}

function generateNFTs() {
  // Get a bigint of possible combinations
  let combinations = new BigNumber(1);
  for (let i=0; i<attributes.length; i++) {
    combinations = combinations.multipliedBy(attributes[i].required ? attributes[i].count : (attributes[i].count + 1));
  }
  console.log(`Possible combinations: ${combinations.toString()}`);

  // generate desired count of different random numbers in the range
  let faceCodes = [];
  while (faceCodes.length < config.desiredCount) {
    let code = getRandomInt(combinations);
    if (!bnIncludes(faceCodes, code))
      faceCodes.push(code);
  }

  // Convert generated codes into NFT properties
  let faces = [];
  for (let i=0; i<faceCodes.length; i++) {
    let arr = codeToArray(faceCodes[i]);
    faces.push(arr);
  }

  // Save faces
  if (!fs.existsSync(config.outputFolder)){
    fs.mkdirSync(config.outputFolder);
  }
  fs.writeFileSync(`${config.outputFolder}/${config.outputJSON}`, JSON.stringify(faces));
}

/**
 * Generate protobuf JSON schema
 */
function generateSchema() {
  // Empty schema
  let schema = {
    nested: {
      onChainMetaData: {
        nested: {
          NFTMeta: {
            fields: {
              traits: {
                id: 1,
                rule: 'repeated',
                type: 'FireballTrait'
              }
            }
          },
          FireballTrait: {
            options: {
            },
            values: {
            }
          }
        }
      }
    }
  };

  // Fill schema with attributes (traits)
  let value = 0;
  for (let i=0; i<attributes.length; i++) {
    for (let j=0; j<attributes[i].count; j++) {
      schema.nested.onChainMetaData.nested.FireballTrait.options[`PROP_${value}`] = `{"en": "${attributes[i].attrNames[j]}"}`;
      schema.nested.onChainMetaData.nested.FireballTrait.values[`PROP_${value}`] = value;
      value++;
    }
  }

  fs.writeFileSync(`${config.outputFolder}/${config.outputSchema}`, JSON.stringify(schema));
}

function main() {
  generateNFTs();
  generateSchema();
}

main();

im try:

  1. Delete node_modules folder

  2. Delete package-lock.json file

  3. Rewrite the package.json file from "react-scripts": "3.x.x" to "react-scripts": "^3.4.1"

  4. Install node packages again npm i

    how to fix it

1 Answers

If you look at the stacktrace, it gives you the exact location of the error: line 64, position 8 of the file

C:\Users\wasab\Desktop\nft\nft-workshop-master\nft-generator.js:64:8

That's the problematic code:

fs.mkdirSync(config.outputFolder);

Apparently, config.outputFolder is undefined at that point. You could confirm this by logging its value before running fs.mkDirSync.

Related