Error: Loki cannot be used as a constructor

Can anyone assist me in understanding why this code is not functioning correctly?

Here's what my index.ts file in Hapi.js looks like:

import { Server, Request, ResponseToolkit } from '@hapi/hapi';
import * as Loki from 'lokijs';

...

const db = new Loki(`${UPLOAD_PATH}/${DB_NAME}`, { persistenceMethod: 'fs' });

This code snippet is pretty straightforward and has been adapted from a well-known example

The scripts section in my package.json appears as follows:

"scripts": {
"prestart": "tsc",
"start": "node dist/index.js",
...

These scripts compile into dist/index.js. However, upon running the 'start' command, I encounter the following error:

TypeError: Loki is not a constructor

I have tried various solutions but seem to be missing the key point. I would greatly appreciate any assistance on how to properly instantiate a new Loki() object and why it works differently elsewhere.

Thank you for your support!

Answer №1

If you're working with LokiJS, you have a couple of options:

import * as Loki from 'lokijs';
const db = new Loki.default(`${UPLOAD_PATH}/${DB_NAME}`, { persistenceMethod: 'fs' });
// Personally, I find this method unattractive.

or

import Loki from 'lokijs';
const db = new Loki(`${UPLOAD_PATH}/${DB_NAME}`, { persistenceMethod: 'fs' });

Upon exploring the internals of lokijs, it appears that the class is exported as module.exports = factory(), which is essentially the same as export default factory(), meaning the class is exported as default. This concept can be better understood by reading this informative discussion on Stack Overflow: ES6: "import * as alias" vs "import alias"

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

Skipping MongoDB in loop operations in a Node.js environment.The original text was modified to

Apologies for the beginner question (The following code is related to express framework and mongoose DB) I am attempting to iterate through the array 'Users' which contains usernames. Then, I am trying to match them in the mongoose database to r ...

Generate an array of identifiers from a pre-existing array

I am facing a challenge where I need to create an array of IDs for each element in an existing array whose size is unknown. The twist here is that every set of four elements should have the same ID. As an illustration, if the original array (let's ca ...

What is the purpose of a Typescript function that returns a function with a generic type?

I recently stumbled upon a perplexing piece of TypeScript code that revolves around the function componentControl. Despite my efforts, I find myself struggling to fully comprehend its purpose and functionality. componentControl: const componentControl: &l ...

Organize elements within an array using TypeScript

I have an array that may contain multiple elements: "coachID" : [ "choice1", "choice2" ] If the user selects choice2, I want to rearrange the array like this: "coachID" : [ "choice2", "choice1" ] Similarly, if there are more tha ...

Attempting to extract a parameter from a URL and pass it as an argument to a function for the purpose of locating objects based on

I am trying to retrieve data from a URL http://localhost:3000/share/user=sampleuser to display objects with an author value that matches the one in the URL. However, I encountered an error when attempting to call a function that extracts the value from t ...

TypeScript fails to detect errors in setting state with incorrect interface properties in React components

Even though I clearly defined an interface with specific props and assigned that interface to be used in useState, no error is triggered when the state value is set to an array of objects with incompatible props: This is how ResultProps is defined: interf ...

Validation Error: Google OAuth Token JSON is Invalid

I have encountered an issue while attempting to validate an access token received from the Android SDK on the server side. I am making a call to the following API endpoint for validation: https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=<tok ...

Error: The argument begins with a non-ASCII dash, suggesting it is likely invalid: [ '- g', 'appium' ]

Whenever I attempt to Install Appium, I am instructed to open Command Prompt or Terminal and enter the following command: npm install –g appium Error Encountered: npm ERR! arg Argument starts with non-ascii dash, this is probably invalid: [ '- g& ...

Having trouble processing images in multi-file components with Vue and TypeScript

Recently, I reorganized my component setup by implementing a multi-file structure: src/components/ui/navbar/ Navbar.component.ts navbar.html navbar.scss Within the navbar.html file, there was an issue with a base64-encoded image <img /> ...

The concept of passing arguments in Typescript

During my experience with Typescript programming, I encountered a situation like the one described below. If I pass an argument containing an object with the same name as the parameter defined in the function signature, Typescript recognizes it, but not ...

Issues arise when upgrading from Angular 8 to 9, which can be attributed to IVY

After successfully upgrading my Angular 8 application to Angular 9, I encountered an error upon running the application. { "extends": "./tsconfig.json", "compilerOptions": { "outDir": ". ...

I keep encountering a 'Missing Permissions' issue whenever I attempt to execute the ban command in Discord.js. What could be causing this problem?

While working on coding a ban command for my Discord server, I encountered an issue where the command was not working as expected. Upon testing, I received an error message on the console stating DiscordAPIError[50013]: Missing Permissions. This was puzzli ...

What is the process for generating an array of objects using JavaScript?

I am struggling to create an array of objects using JavaScript and facing errors with new lines added where I need to split the messages and collect row numbers. The row numbers should be comma-separated if it is a repetitive error message. I found a solu ...

Difficulties arise when attempting to install nodejs on linux using the npm install nodejs command

While attempting to execute... npm install nodejs I encounter the subsequent issue: npm WARN optional Skipping failed optional dependency /chokidar/fsevents: npm WARN notsup Not compatible with your operating system or architecture: [email prot ...

Looking for a demonstration using dust.js or handlebars.js in a two-page format with express3.x and node?

Currently, I am in the process of selecting a templating engine to use. While I have come across numerous single-page examples utilizing template engines, I am specifically searching for a practical example that demonstrates handling two distinct pages whi ...

Sending a style prop to a React component

My typescript Next.js app seems to be misbehaving, or perhaps I'm just not understanding something properly. I have a component called <CluckHUD props="styles.Moon" /> that is meant to pass the theme as a CSS classname in order to c ...

Is there a way to verify an if-else statement in the ngStyle background property with Angular 7?

I have a collection of cards that need to be shown in a component. Each card contains a cover-image with an URL fetched from the server. In my component.html, I am using ngFor as follows: <div [style.background-image]="'url('+row.companyId?.c ...

Why is the message ID missing in Firebase Cloud Messaging?

I am currently in the process of upgrading from legacy HTTP to v1 using the NPM firebase-admin module. The issue I am facing is that when I send a notification, there are no errors thrown, but neither do I receive a message-id and the notification does not ...

What is the best way to emphasize specific months and years in an Angular Material datepicker?

I have an array of days, which can be from any year. I am attempting to customize the Angular Material datepicker to highlight specific months and years in the selection views based on the array of days. .html <input [matDatepicker]="picker" ...

Prevent identical values from being added repeatedly to a table while updating in real-time

Currently, I am facing an issue while running through a loop to extract values from an array. When I add a second value, it duplicates both the new and previous values in the table. For example, adding a row with value 488 works fine: <tr class="exe"& ...