Error: The "require" function is undefined and cannot be recognized in app.js on line 3

Encountering difficulties in connecting front-end HTML to a private blockchain for interacting with a smart contract.

Steps completed thus far:

  • Created a smart contract and deployed it on the private blockchain.
  • npm install -g web3
  • Developed an HTML file and connected it to the following JS file.

// Sample app.js code snippet

var Web3 = require('web3');
var web3 = new Web3();

if (typeof web3 !== 'undefined') {alert(" web3.currentProvider :: " + web3.currentProvider);
    web3 = new Web3(web3.currentProvider);
} else {
    // Choose the desired provider from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
 }

 var abi= My-custom-ABI;


 var ins=web3.eth.contract(abi);
 var instance= ins.at("My-Contract-Address");
 var accounts;
 var account;
 var account2;

Answer №1

To incorporate it into your project, make sure to include it as a dependency by running npm install --save web3. Additionally, you will need webpack for transpiling your JavaScript code since browsers do not recognize the require syntax.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the solution for the warning "Solid's reactivity is broken when destructuring component props"?

Just starting out with SolidJS and encountering an issue with my UI setup import { render } from "solid-js/web"; import { createSignal, Show } from "solid-js"; import { createStore } from 'solid-js/store'; function Form() { ...

Difficulty encountered while configuring ngx-lightbox within an Angular module

Greetings! I am relatively new to working with Angular and currently in the process of creating a website that requires lightbox galleries within each component. Upon seeking advice, ngx-lightbox was recommended as a solution for my photo gallery needs. Ho ...

Toggle the display of dropdown 2 or dropdown 3 depending on the option chosen in dropdown 1

I am struggling with a form that contains 3 dropdowns: <select id="1" required> <option value="">Choose an option</option> <option value="1">Apple</option> <option value="2">Orange ...

Is it possible to retain various delimiters after dividing a String?

In the code below, the someString gets split into an array using specified delimiters in separators var separators = ['\\.', '\\(', '\\)', ':', '\\?', '!&apos ...

Ways to extract a string value from a JSON object

My JSON object looks like this: [{"query":"Apple"}] Is there a way for me to extract the value "Apple" from this object and store it in a variable? I'm specifically working with NodeJS. ...

Accessing JSON data in Node.js

I'm completely new to working with Node.js and I've been trying to figure out the best way to parse and query a JSON object. Recently, I came across this JSON object that I have loaded as a file: [ {"Key":"Accept","Values":["Application/x-www-fo ...

Vue component fails to react to updates from Vuex

Currently, I am developing a system to facilitate the management of orders at a shipping station. Although I have successfully implemented the initial changes and most of the functionality, I am encountering an issue where one component fails to update ano ...

JavaScript validation for a form dynamically generated within a table

NOTE: Utilizing Foundation 6 along with the Abide Form Validation. I'm currently working on implementing automatic form validation for a website. The approach I've taken involves creating a table (using the jQuery Datatables library) with multip ...

Can the color of an ion-radio be adjusted when it is not selected?

I am experiencing an issue where I need to change the default color (grey) to a specific color of my choice, such as the primary color. I have attempted the following methods: <ion-col sizeXs="12" sizeMd="6" class="radio-box"> <ion-item line ...

What could be causing the malfunction of this Bootstrap button dropdown?

Initially, I attempted using regular HTML for the dropdown button but encountered issues. As a result, I switched to jsfiddle to troubleshoot. Despite my efforts, the dropdown feature still refused to work. If you'd like to take a closer look, here&a ...

Convert a web page with embedded images using Base64 strings into a PDF file using JavaScript

My website has numerous images with base 64 string sources. Typically, I am able to download the HTML page as a PDF using a service method that involves sending the HTML page with a JSON object and receiving the PDF in return. However, when there are many ...

Is there a way to access a random element from an array upon pressing a button?

My dilemma involves a reducer array containing Q/A objects that are displayed in two different divs. I want to create a function that will be triggered by the "Next" button, generating a random number to determine which object from the array is displayed n ...

Storing a variable in Vue on one page and accessing it on another page

Is there a way to transfer a value from config.js file to Geturl.vue? The content of the config.js file is as follows: var myUrl = "http://localhost:7070/#/"; The code snippet in my view file (Geturl.vue) is shown below: <div id="app>> <p ...

Babel had trouble transpiling due to an unexpected token while working with Bootstrap 4 and Codekit

Currently attempting to minify and transpile Bootstrap 4 using CodeKit3. However, encountering the following error: Babel: Failed to Transpile: SyntaxError: /bootstrap/carousel.js: Unexpected token (219:8) 217 | _getConfig(config) { 218 | ...

What is the best way to perform unit testing on a function component that includes React.useState() using jest and enzyme?

I'm working on a function component that utilizes React.useState() to handle the state of a drawer modal. My challenge lies in testing this function and its ability to modify state using jest enzyme, as I cannot access its state function due to it not ...

The tab indicator in Material-UI fails to update when the back button is clicked

My code is currently functioning well: The tab indicator moves according to the URL of my tab. However, there is a peculiar issue that arises when the back button of the browser is pressed - the URL changes but the indicator remains on the same tab as befo ...

Utilizing Javascript to implement a tooltip feature for dynamically inserted text

I recently incorporated a brief jQuery tooltip plugin into my site. It consists of approximately ten lines of code and works smoothly, as demonstrated in this demo. However, I encountered an issue when attempting to add new text that should also trigger t ...

If there is only one item, jCarousel will not display anything

After incorporating jCarousel into jQuery ui Tabs, I created a page which can be found at the following link: The issue arises when clicking on the 3rd tab (containing only one item), as nothing is visible until the left arrow is clicked to navigate back. ...

Utilize Node Package to Dispatch Firebase Push Notifications

I am currently experimenting with a NodeJS module for sending Push Notifications. I have been exploring the 'node-sender' module but have not been able to find an option for sending notifications to a web browser for testing purposes: https://w ...

Uncertainty arises from the information transmitted from the controller to the Ajax function

In one of my coffee scripts, I have an AJAX call that calls a method in a controller. The AJAX call is structured like this: auto = -> $.ajax url : '<method_name>' type : 'POST' data : <variable_name> ...