最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer,

programmeradmin3浏览0评论

Once I tried running:

node app.js

I got the following error message:

buffer.js:330
  throw new ERR_INVALID_ARG_TYPE(
  ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at Function.from (buffer.js:330:9)
    at Object.<anonymous> (C:\DappUniversity-web3js\app.js:8:28)
    at Module._pile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Here's what I wrote in the file app.js:

app.js

var Tx = require('ethereumjs-tx')
const Web3 = require('web3')
const web3 = new Web3('')

const account1 = '0x31C662426bA57409b3F53ececAbcfA7c6EA43163'
const account2 = '0xa00739C3A4B34C4D2Cb66386d8377EE951521e86'

const privatekey1 = Buffer.from(process.env.PRIVATE_KEY_1)
const privatekey2 = Buffer.from(process.env.PRIVATE_KEY_2)

web3.eth.getBalance(account1, (err, bal) => {
    console.log('account 1 balance:', web3.utils.fromWei(bal,'ether'))
})

web3.eth.getBalance(account2, (err, bal) => {
    console.log('account 2 balance:', web3.utils.fromWei(bal,'ether'))
})

I was just trying to get the balance of the two account addresses but kept getting the error. Although I did try to uninstall the npm packages web3 and ethereumjs-tx, it still did not solve this issue.

So, what should be done to rectify this error? Did I do something wrong here or is it because of some missing npm package files or details?

Once I tried running:

node app.js

I got the following error message:

buffer.js:330
  throw new ERR_INVALID_ARG_TYPE(
  ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
    at Function.from (buffer.js:330:9)
    at Object.<anonymous> (C:\DappUniversity-web3js\app.js:8:28)
    at Module._pile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Here's what I wrote in the file app.js:

app.js

var Tx = require('ethereumjs-tx')
const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/v3/0aa71df89e464ba9b0a3b35449346dac')

const account1 = '0x31C662426bA57409b3F53ececAbcfA7c6EA43163'
const account2 = '0xa00739C3A4B34C4D2Cb66386d8377EE951521e86'

const privatekey1 = Buffer.from(process.env.PRIVATE_KEY_1)
const privatekey2 = Buffer.from(process.env.PRIVATE_KEY_2)

web3.eth.getBalance(account1, (err, bal) => {
    console.log('account 1 balance:', web3.utils.fromWei(bal,'ether'))
})

web3.eth.getBalance(account2, (err, bal) => {
    console.log('account 2 balance:', web3.utils.fromWei(bal,'ether'))
})

I was just trying to get the balance of the two account addresses but kept getting the error. Although I did try to uninstall the npm packages web3 and ethereumjs-tx, it still did not solve this issue.

So, what should be done to rectify this error? Did I do something wrong here or is it because of some missing npm package files or details?

Share Improve this question edited Sep 7, 2022 at 18:34 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Dec 7, 2021 at 7:53 BuiltockchainBuiltockchain 391 gold badge1 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The error is related to either of these lines:

const privatekey1 = Buffer.from(process.env.PRIVATE_KEY_1)
const privatekey2 = Buffer.from(process.env.PRIVATE_KEY_2)

You're trying to create a Buffer from an invalid (most likely undefined) value.

process.env.<variable_name> are by default only loaded from the environment variables (e.g. passed by your system, by Docker container, or by mand line arguments).

Example:

# sets the `process.env.PRIVATE_KEY_1` value in the node script
PRIVATE_KEY_1=123456 node index.js

It doesn't automatically load the .env file contents. For that, you can use the dotenv package.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论