Module not found: Can't resolve 'moralis/types' in

i get this erroor

Module not found: Can’t resolve ‘moralis/types’ in
i have installed moralis using npm install moralis and imported moralis/types

import React, { useState } from 'react';
import styled from 'styled-components';
import PropTypes from 'prop-types';
import { useMoralis } from "react-moralis";
import Moralis from 'moralis/types';

i am in learning process and wanted to practice moralis object

a function to new instance to moralis object

const Orders =  async () =>{
    const OrdersList = Moralis.Object.extend('OrdersList');
    const orderz = new OrdersList ();

You can try to look at this documentation about how to use react with Moralis:

where did you find this line in documentation?

imported auto by moralis code snippest

I see this in documentation: const { Moralis, isInitialized, } = useMoralis();

thank you, back to the documentation

useMoralis() is a hook, hooks can only be used within react components or hooks

sometimes you have helper functions that need moralis but they are not defined within any react component or hook

I assume you are using the Moralis React Boilerplate.

If you want to use Moralis independent of React, you need to do 2 things:

import Moralis from "moralis";
in the file where you have
const OrdersList = Moralis.Object.extend('OrdersList');

Initialize Moralis with your server info as documented here, under the section Initialize the SDK.

This would be your solution if you intend to use the Orders() function outside of the React App. Probably not the case.

You probably want to use Orders() inside of the React App.
So here is the best solution, IMO;

Use Moralis hooks.
You have a Moralis object that is initialized and accessible within the <MoralisProvider> in index.js.
This Moralis object is the one you get when you call the useMoralis() hook.

If you can do

const {Moralis} = useMoralis();

const OrdersList = Moralis.Object.extend('OrdersList');

then that’s your solution.

If this solution fails to build, read on:
There is a chance you can’t do this in your .js file code.
e.g. because that code is outside of a React component or a React Hook, like in a file with helper functions.

In this case, I assume that the Orders() function is still eventually used in a React component or hook.Then you can get your “React”-Moralis Object from there and pass it into your function:

const Orders =  async (Moralis) =>{
    const OrdersList = Moralis.Object.extend('OrdersList');
    const orderz = new OrdersList ();
1 Like