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

ReferenceError: File is not defined #50884

Closed
Annastacia-dev opened this issue Jan 26, 2024 · 1 comment
Closed

ReferenceError: File is not defined #50884

Annastacia-dev opened this issue Jan 26, 2024 · 1 comment

Comments

@Annastacia-dev
Copy link

Annastacia-dev commented Jan 26, 2024

I'm using the activestorage npm package in a NextJs app
I have imported the DirectUpload component import { DirectUpload } from 'activestorage';
Error emerges when this code to upload the file is hit

const uploadFile = (file: RcFile) => {
    const upload = new DirectUpload(
      file,
      `${api.defaults.baseURL}/rails/active_storage/direct_uploads`
    );
    upload.create((error: Error, blob: ActiveStorage.Blob) => {
      if (error) {
        console.error(error);
      } else {
        console.log(blob)
      }
    });
  };

Error stack:

Import trace for requested module:
./node_modules/activestorage/app/assets/javascripts/activestorage.js
./src/components/admin/forms/products/NewProduct.tsx
 ✓ Compiled in 1015ms (3194 modules)
 ⚠ Fast Refresh had to perform a full reload due to a runtime error.
 ⨯ ReferenceError: File is not defined
    at /home/annastacia/webcafe/dawatele/web/node_modules/activestorage/app/assets/javascripts/activestorage.js:438:19

I have used this before in a Vite + ReactJS app recently and I don't encounter the same error

@Edouard-chin
Copy link
Member

Ah it's because activestorage isn't intended to be used in a node.js environment. Node doesn't implement the File API.

This issue is similar #34586 and you should be able to find a workaround from other users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants