Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Can't resolve 'fs' #464

Closed
hluo-alt opened this issue Nov 16, 2023 · 7 comments
Closed

[Bug] Can't resolve 'fs' #464

hluo-alt opened this issue Nov 16, 2023 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@hluo-alt
Copy link

The issue
When I try to import the Casbin Enforcer, it gives this error:

./node_modules/casbin/lib/esm/enforcer.js:653:27-39 - Error: Module not found: Error: Can't resolve 'fs' in '<PROJECT_DIR>/node_modules/casbin/lib/esm'

I am using Casbin in node.js with node version v16.17.0 and Angular version v14.

Desktop:

  • OS: Ubuntu 22.04.3 LTS
  • Browser: chrome
@hluo-alt hluo-alt added the bug Something isn't working label Nov 16, 2023
@casbin-bot
Copy link
Member

@tangyang9464 @JalinWang

@hsluoyz hsluoyz transferred this issue from casbin/casbin Nov 17, 2023
@casbin-bot
Copy link
Member

@nodece @Shivansh-yadav13

@nodece
Copy link
Member

nodece commented Nov 18, 2023

Hi @hluo-alt, please add the following config to package.json:

  "browser": {
    "fs": false
  }

Casbin examples: https://github.com/node-casbin/casbin-examples

@hluo-alt
Copy link
Author

Thank you @nodece
I have added the config like you said, but this time it is giving me a new error Uncaught ReferenceError: Buffer is not defined in the Chrome console. Do you have any idea what to do next?

@nodece
Copy link
Member

nodece commented Nov 20, 2023

Buffer is built-in library for the Node.js project.

In the Web, you can install this library by “yarn add buffer”.

@hluo-alt
Copy link
Author

Thanks, in addition to installing Buffer, I added

import * as Buffer from 'node_modules/buffer';
(window as any).Buffer = Buffer.Buffer;

in polyfills.ts to get it working.

@frisia-mtz
Copy link

For people using vite, the plugin vite-plugin-node-polyfills helps alleviate the issue with Buffer in the browser.

The following vite.config.ts worked for me:

import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    nodePolyfills({
      globals: { Buffer: true },
    }),
  ],
  // other config
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

5 participants