Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
ReactJS Library being used in NextJS project says is not a constructor

I have built a ReactJS library and published it to NPM and works fine in a standard ReactJS project, but I now have a NextJS and I am trying to add it there, I expected it would just work as NextJS is a framework on top of ReactJS with some additional functionality.

In the app.js I have the following:

import dynamic from 'next/dynamic'
import {useEffect} from "react";

function MyApp({ Component, pageProps }) {

    const {CrashCatch, CrashCatchProvider} = dynamic(import('crashcatchlib-reactjs'), { ssr: false })

    let crash_catch = new CrashCatch();

    crash_catch.initialiseCrashCatch("12978650", "d7kqcewvshoyxbp564f8tm0zl", "1.0.1");

    return (
       ....
    ) 
}

When running this I get TypeError: CrashCatch is not a constructor

I have tried not using a dynamic import and doing a standard import as import {CrashCatch, CrashCatchProvider} from "crashcatchlib-reactjs"; but then I get the error SyntaxError: Cannot use import statement outside a module]

The source code for the reactjs library is open source so it's available on GitHub at https://github.com/Crash-Catch/CrashCatchLib-ReactJS. The index.js has the class CrashCatch which has the constructor so not sure why NextJS is treating it differently.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  • I never used crashcatchlib-reactjs library. I checked the docs could not understand how they import it. But to solve this error:

    SyntaxError: Cannot use import statement outside a module]
    

Install https://www.npmjs.com/package/next-transpile-modules. If you check the docs, in next.config.js

// next.config.js
const withTM = require('next-transpile-modules')(['crashcatchlib-reactjs']); 
module.exports = withTM({});
  • If it does not solve the issue, try dynamic import way, however, one think you should know dynamic import returns Loadable Component. I checked dynamic import works only for components. In order to use import stament with that package, you should still add configuration of next-transpile-modules. Then create a component:

import {CrashCatch } from "crashcatchlib-reactjs";

export default function ClientSideComponent(props){
    // Write here the logic.
 
    return <>yourJsxLogicComponent</>;
}

Then import this component dynamically:

const ClientSideComponent = dynamic(()=> import("./ClientSideComponent.js"), { ssr: false });
  • In this case use vanilla js dynamic import. I am not sure If you have to add configuration of next-transpile-modules.

    let CrashCatchImport, CrashCatchProviderImport;
    
    import("crashcatchlib-reactjs").then((CrashCatch, CrashCatchProvider) => 
    {
      CrashCatchImport = CrashCatch.default;
      CrashCatchProviderImport=CrashCatchProvider.default;
    });
    
over 4 years ago · Santiago Trujillo Report

0

NextJS executes code on the server-side, so your library should also be compatible with Node. It seems import is not supported in CommonJS, which is the default module format used by Node. However, you can specify that your library should be treated as an ES Module to support import statements as described in the documentation https://nodejs.org/api/esm.html#enabling

Node.js treats JavaScript code as CommonJS modules by default. Authors can tell Node.js to treat JavaScript code as ECMAScript modules via the .mjs file extension, the package.json "type" field, or the --input-type flag. See Modules: Packages for more details.

You could try setting the type property to module in your package.json

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!