I encountered an issue while attempting to establish a connection to an API using WebSocket. Specifically, I received an error message stating: "Uncaught ReferenceError

Guide on Installing the API from GitHub;

const WebSocket = require("ws");
const DerivAPI = require("@deriv/deriv-api/dist/DerivAPI");

// Use your own app_id instead of 1089 for testing
// Register your own app at api.deriv.com to get a unique app_id.
const connection = new WebSocket(
    "wss://ws.binaryws.com/websockets/v3?app_id=1089"
);
const api = new DerivAPI({ connection });
const basic = api.basic;

basic.ping().then(console.log);

Encountered ReferenceError: require is not defined

Answer №1

If you encounter the error message

"ReferenceError require is not defined"
, one solution is to remove the type property set to module in your package.json file and change any files with a .mjs extension to have a .js extension.

package.json

{
  // 👇️ this should be removed if you want to use `require`
  "type": "module",
  // ... 👇️ rest
}

Alternatively, you can opt for the ES6 module syntax using the import and export keywords.

In case you prefer utilizing the import/export syntax for modules, make sure to include the type property as module in your package.json file.

package.json

{
  // 👇️ add this
  "type": "module",
  // ... 👇️ rest
}

This adjustment involves replacing the require and module.exports syntax with the import and export keywords.

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

Having issues with passing POST data during file handling operations

I am attempting to utilize Ajax POST on my WordPress website to check if checkboxes are selected and update their values in the database upon clicking a button. Although using the standard PHP form submission is the simplest method, the fields that I need ...

The function causes changes to an object parameter once it has been executed

I've encountered an issue with a function that is supposed to generate a string value from an object argument. When I call this function and then try to use the argument in another function, it seems to be getting changed somehow. Here is the code fo ...

Storing a collection (of 'keywords') in MongoDB with Mongoose

Currently, I am experimenting with Mongoose and facing some challenges when it comes to saving data to an array. Specifically, I have an input field on a webpage where users can enter comma-separated tags. After obtaining these tags from req.body.tags, rem ...

The headers set in jQuery's $.ajaxSetup will be used for every ajaxRequest, even if they

Below are the parameters set for all ajax calls in the <head> of my document. (This is necessary to fix an iOS ajax bug referenced at $.ajaxSetup ({ cache: false, headers: { "cache-control": "no-cache" } }); I am wo ...

What are the solutions for fixing a JSONdecode issue in Django when using AJAX?

I am encountering a JSONDecodeError when attempting to send a POST request from AJAX to Django's views.py. The POST request sends an array of JSON data which will be used to create a model. I would greatly appreciate any helpful hints. Error: Except ...

Ensure that the Promise is resolved upon the event firing, without the need for multiple event

I'm currently working on a solution where I need to handle promise resolution when an EventEmitter event occurs. In the function containing this logic, an argument is passed and added to a stack. Later, items are processed from the stack with differe ...

Image encoded in base64 not appearing on the screen

Hey there, I'm currently working on implementing a jQuery image uploader that generates a base64 code when an image is selected. function readImage(input) { if (input.files && input.files[0]) { var FR= new FileReader(); FR.onload ...

Adding an item to a collection using NgRx

I am working with a state that has a specific structure. It consists of a list of Workouts, and each Workout contains a list of exercises associated with it. I have two main objectives: To add new exercises to a particular workout from the existing list o ...

Interested in integrating server side JavaScript with your Android application?

Running a simple web page on a Raspberry Pi to toggle the board's LED with the click of a button. The button triggers a JavaScript function that controls the LED. Now, I want to be able to call this script from a button in an Android application... B ...

Does it make sense to prioritize robust $routeProviders and keep controllers slim?

Traditionally in MVC architecture, models are designed to be robust and controllers are kept minimal for easier testing. However, Angular lacks a clear model structure, making it challenging to organize code for reuse. Although Angular does offer services ...

Having difficulty installing web3 with npm on my Windows 10 system

I followed all the instructions provided on the following page: https://medium.com/@PrateeshNanada/steps-to-install-testrpc-in-windows-10-96989a6cd594 Everything worked well except for step number 5 where my Node.js version is 8.10.0 instead of 8.10.2, w ...

Struggling to retrieve the most recent emitted value from another component in Angular?

Hello everyone, I am currently attempting to retrieve the most recent updated value of a variable from the component app-confirm-bottom-sheet in the app-bene-verification.ts component, but unfortunately, I am unable to achieve this. Below is the code snipp ...

Tips for displaying a child React component for a specific duration

One of the React components I have is called PopUpBanner, which is utilized to display messages. For instance, in my login component, I employ it in this manner. If an error arises, the bannerMessage state is updated to showcase the message on the banner: ...

Encoding and Displaying Characters in HTML with UTF-8

I am experiencing an issue with UTF-8 characters in the page title. I would like to include the music symbol ♫ on the page title. The strange thing is that sometimes it displays correctly (in Google Chrome) and other times it shows a square symbol indic ...

Updating Elements in an Array Using JavaScript is Not Functioning as Expected

In my Angular application, I have included some lines of TypeScript code which involve Boolean variables in the constructor and an array of objects. Each object in this array contains input variables. selftest: boolean; failed: boolean; locoStateItem ...

Converting a Finsweet hack #4 from jQuery to pure JavaScript in Webflow: Step-by-step guide

I have a jQuery code that I need to convert to pure JavaScript. Can you assist me in translating this code into pure JavaScript? I have left comments in the code to make it easier. ORIGINAL JQUERY CODE: // When the DOM is ready document.addEventListener(& ...

Can the nodemailer package be used with alternative services to send emails?

After reviewing the nodemailer documentation, I discovered that they do not support unoeruo mails as a service. Is there a way to bypass this limitation or should I consider using a different package? If so, do you have any recommendations? ...

Conceal the Addon Tab across all pages in Storybook

Is there a way to globally hide an Addon Tab without disabling the addon itself? Specifically, I am using version 5.3.18 of Storybook with React and I want to hide the tab panel from the addon "styled-component theme". I apologize for the basic question, ...

What's causing this MUI React data grid component to be rendered multiple times?

I have developed a wrapper for the MUI Data Grid Component as portrayed: Selection.tsx: import * as React from 'react'; import { DataGrid, faIR, GridSelectionModel } from '@mui/x-data-grid'; import type {} from '@mui/x-data-grid/t ...

Using a function as an argument within an Angular directive

Looking for a solution to pass a promise-returning function into a directive? Here's what I'm currently doing: In the parent controller, I've created a callback: $scope.myCb = function(data) { console.log(data); } Directive Scope: sco ...