Error occurred while pinning file to IPFS: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string

Viewed 362

When i add the files to another project I get the following error and i can't figure out why.

When I run "node scripts1/runScript.js" error i get is:

Error occurred while pinning file to IPFS:  TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:119:11)
    at Url.parse (node:url:169:3)
    at Object.urlParse [as parse] (node:url:156:13)
    at dispatchHttpRequest (C:\Users\alaiy\Documents\NFTs\nft-mix-main\node_modules\axios\lib\adapters\http.js:118:22)
    at new Promise (<anonymous>)
    at httpAdapter (C:\Users\alaiy\Documents\NFTs\nft-mix-main\node_modules\axios\lib\adapters\http.js:48:10)
    at dispatchRequest (C:\Users\alaiy\Documents\NFTs\nft-mix-main\node_modules\axios\lib\core\dispatchRequest.js:58:10)
    at Axios.request (C:\Users\alaiy\Documents\NFTs\nft-mix-main\node_modules\axios\lib\core\Axios.js:108:15)
    at wrap (C:\Users\alaiy\Documents\NFTs\nft-mix-main\node_modules\axios\lib\helpers\bind.js:9:15) {
  code: 'ERR_INVALID_ARG_TYPE'
}

The runScript.js code is :

const path = require('path');
const pinFileToIPFS = require('./pinFileToIPFS');

const filePath = path.join(__dirname, '../img/Toad-Licking-Mario.jpg');
// const filePath = path.join(__dirname, '../data/metadata.json');

pinFileToIPFS(filePath);

The pinFileToIPFS.js file which contains the "url: pinataEndpoint" i think the error message is referring too:

require('dotenv').config();
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data');
const { storeDataToFile } = require('./ipfsHelper.js');

// Calls Pinata API's to pin file to IPFS
const pinFileToIPFS = async (filePath) => {
  const pinataEndpoint = process.env.PINATA_ENDPOINT;
  const pinataApiKey = process.env.PINATA_API_KEY;
  const pinataApiSecret = process.env.PINATA_API_SECRET;
  const form_data = new FormData();
  try {
    form_data.append('file', fs.createReadStream(filePath));
    const request = {
      method: 'post',
      url: pinataEndpoint,
      maxContentLength: 'Infinity',
      headers: {
        pinata_api_key: pinataApiKey,
        pinata_secret_api_key: pinataApiSecret,
        'Content-Type': `multipart/form-data; boundary=${form_data._boundary}`,
      },
      data: form_data,
    };
    console.log('request:', request);
    const response = await axios(request);
    console.log('Successfully pinned file to IPFS : ', response);
    await storeDataToFile(response.data);
    console.log('Successfully added IPFS response to json file');
  } catch (err) {
    console.log('Error occurred while pinning file to IPFS: ', err);
  }
};

module.exports = pinFileToIPFS;

Lastly my .env file is:

# export PRIVATE_KEY=asafdagadd
# export WEB3_INFURA_PROJECT_ID=asdfsdf



# export ETHERSCAN_TOKEN=asdfasdfasdf
export IPFS_URL=blah blah
export UPLOAD_IPFS=true
export PINATA_API_KEY='blah blah'
export PINATA_API_SECRET='blah blah'
export PINATA_ENDPOINT="https://api.pinata.cloud/pinning/pinFileToIPFS"

I can upload the package.json file aswell if needed.

Any help is appreciated!

SOLUTION: Turned out the export syntax I was using in my .env file should not be there. export syntax is for python scripts which I was previously using.

As I am running javascript scripts, I deleted the export syntax and everything worked like a charm!

1 Answers

I was facing same error, the i find out that my config folder suppose to be in the folder in which I was executing it from. This error comes when your url value is not getting the env variables.

So in my case solution was something like this-

config/

--default.json

scripts/

--server.js

So it was searching config folder inside scripts folder.

Later I run my file outside from scripts folder then it worked fine.

node scripts/server.js

Related