Skip to content

sweep(slow): fix the build errors caused when using next.js and typescript.Β #1022

@rikhoffbauer

Description

@rikhoffbauer

React or Next.js build gives error for custom implementation

To Reproduce Steps to reproduce the behavior:

  1. Go to create a next.js project with typescript
  2. Write the following code:
import { create } from 'react-abac';
import { Permission } from './permissions';
import { Role } from './roles';

type User = {
    id: string;
    name: string;
    rules: typeof Rules;
    permissions: Permission|;
    role: Role[];
}

export const {
    AllowedTo,
    secured,
    NotAllowedTo,
    AbacContextDefaults,
    AbacProvider,
    AbacContext,
    useAbac,
} = create<Role, Permission, User>();
{
    ...
    "dependencies": {
        ...
        "react-abac": "^0.1.13",
        ...
    },
    ...
}
  1. run next build.
  2. See error
ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a 'js' file extension and '/src/node_modules/react-abac/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the 'cjs' file extension.

Attempted import error: 'create' is not exported from 'react-abac' (imported as 'create').

A temp fix for next.js:

const reactAbac = dynamic(() => import("react-abac"), { ssr: false });
reactAbac.create;

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcidependenciesPull requests that update a dependency filesweepAssigns Sweep to an issue or pull request.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions