rails: Active Storage with Direct Upload js giving syntax error
My goal
1 - Starts to upload to the cloud after the moment it is attached on client side (before clicking on ‘update’ button)
2 - Integrate the upload view with another third party js library (as uppy)
From rails documentation: https://edgeguides.rubyonrails.org/active_storage_overview.html#direct-uploads
If you want to use the Direct Upload feature from a JavaScript framework, or you want to integrate custom drag and drop solutions, you can use the DirectUpload class for this purpose. Upon receiving a file from your library of choice, instantiate a DirectUpload and call its create method. Create takes a callback to invoke when the upload completes.
import { DirectUpload } from "@rails/activestorage"
const input = document.querySelector('input[type=file]')
// Bind to file drop - use the ondrop on a parent element or use a
//  library like Dropzone
const onDrop = (event) => {
  event.preventDefault()
  const files = event.dataTransfer.files;
  Array.from(files).forEach(file => uploadFile(file))
}
// Bind to normal file selection
input.addEventListener('change', (event) => {
  Array.from(input.files).forEach(file => uploadFile(file))
  // you might clear the selected files from the input
  input.value = null
})
const uploadFile = (file) => {
  // your form needs the file_field direct_upload: true, which
  //  provides data-direct-upload-url
  const url = input.dataset.directUploadUrl
  const upload = new DirectUpload(file, url)
  upload.create((error, blob) => {
    if (error) {
      // Handle the error
    } else {
      // Add an appropriately-named hidden input to the form with a
      //  value of blob.signed_id so that the blob ids will be
      //  transmitted in the normal upload flow
      const hiddenField = document.createElement('input')
      hiddenField.setAttribute("type", "hidden");
      hiddenField.setAttribute("value", blob.signed_id);
      hiddenField.name = input.name
      document.querySelector('form').appendChild(hiddenField)
    }
  })
}
Expected behavior
Load js files normally
Actual behavior

This line of code is:
 var upload = new !(function webpackMissingModule() { var e = new Error("Cannot find module '@rails/activestorage'"); e.code = 'MODULE_NOT_FOUND'; throw e; }())(file, url);
My configuration
Rails version: 5.2.3 Ruby 2.4.5 Obs: Active Storage itself is working properly
links
this issue I also posted on stackoverflow: https://stackoverflow.com/questions/56132477/active-storage-direct-uploader-giving-syntax-error
About this issue
- Original URL
 - State: closed
 - Created 5 years ago
 - Comments: 37 (8 by maintainers)
 
Commits related to this issue
- `node_modules` is no longer compiled by default fixes https://github.com/rails/rails/issues/35501 fixes https://github.com/rails/webpacker/issues/2131 fixes https://github.com/rails/rails/issues/36278... — committed to jakeNiemiec/webpacker by jakeNiemiec 4 years ago
 - `node_modules` is no longer compiled by default (#2624) fixes https://github.com/rails/rails/issues/35501 fixes https://github.com/rails/webpacker/issues/2131 fixes https://github.com/rails/rails/i... — committed to rails/webpacker by jakeNiemiec 4 years ago
 - `node_modules` is no longer compiled by default (#2624) fixes https://github.com/rails/rails/issues/35501 fixes https://github.com/rails/webpacker/issues/2131 fixes https://github.com/rails/rails/i... — committed to KingTiger001/Rails-web-pack-project by KingTiger001 4 years ago
 - `node_modules` is no longer compiled by default (#2624) fixes https://github.com/rails/rails/issues/35501 fixes https://github.com/rails/webpacker/issues/2131 fixes https://github.com/rails/rails/i... — committed to smartech7/ruby-webpacker by jakeNiemiec 4 years ago
 
@jakeNiemiec Slightly off topic but maybe not. Do you know if this effects production deployments? I deployed my app to heroku for demoing and it’s not reading my trix file which is in the node_modules. Just curious if you know if there needs to be a modification for a production environment like heroku.
@jakeNiemiec that’s fixed it, thanks!
@jakeNiemiec that’s precisely where I got the solve from! I’m a bit too in the weeds right now to have remembered where I got it from 🤣