Search code examples
javascriptnode.jsweb3js

Why do I get this error every time I run a java script file from the command prompt? "reference node is not defined"


JavaScript code:

node 
const Web3 = require('web3')
const web3 = new Web3('network-link')
console.log(web3.eth.accounts.create())

Now I saved this file in the desktop: file.js

Command prompt code:

path/path/path/path
cd Desktop
path/path/path/path/Desktop
node file

This outputs:

ReferenceError: node is not defined
    at Object.<anonymous> (C:\Users\HP\Desktop\file.js:1:1)
[90m    at Module._compile (internal/modules/cjs/loader.js:956:30)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:812:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:724:14)[39m
[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)[39m
[90m    at internal/main/run_main_module.js:17:11[39m

Solution

  • You need to remove node in first line of your code, after refactoring it will be as

    const Web3 = require('web3')
    const web3 = new Web3('network-link')
    console.log(web3.eth.accounts.create())
    

    Because you neither declared node as a variable nor as a function. So when file gets run then node is undefined.