Registry / web-framework / file-drops

file-drops

JSON →
library0.7.0jsnpmunverified

File-drops is a straightforward, in-browser utility designed to simplify handling file drop events within web applications. It abstracts away the complexities of browser drag-and-drop APIs, allowing developers to easily register a drop handler on any DOM element. When files are dropped, the utility processes them, providing an array of objects containing file `name` and `contents`. The current stable version is `0.7.0`. The package generally sees maintenance updates for dependency bumps or minor fixes, with a significant architectural shift in `v0.6.0` which transitioned it to an ES module-only distribution and introduced official TypeScript type definitions, dropping legacy UMD support. Its primary differentiator lies in its focused scope and minimal API, providing a lean solution for file input without encompassing broader UI components or file system interactions, making it highly suitable for applications requiring only direct file content access upon a user drop.

npm install file-drops
INSTALL
IMPORT
SIG · FILE-DROPS
F
file-drops
web-frameworkjavascriptv0.7.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fileDrop
import fileDrop from 'file-drops';
const fileDrop = require('file-drops');
The package migrated to ES Modules (ESM) in v0.6.0, dropping CommonJS (CJS) and UMD distributions. Direct `require()` statements will fail.
FileDropHandler
import type { FileDropHandler } from 'file-drops';
TypeScript types are available since v0.6.0 and should be imported as types.
FileContent
import type { FileContent } from 'file-drops';
TypeScript types are available since v0.6.0 for the structure of returned file objects.

Demonstrates how to initialize a file drop area on a DOM element and handle the dropped files, logging their names and contents.

import fileDrop from 'file-drops'; const container = document.createElement('div'); container.id = 'container'; container.style.width = '300px'; container.style.height = '200px'; container.style.border = '2px dashed #ccc'; container.style.display = 'flex'; container.style.alignItems = 'center'; container.style.justifyContent = 'center'; container.textContent = 'Drag files here'; document.body.appendChild(container); const dropHandler = fileDrop('Drop a file', function(files) { console.log('Files dropped:', files); files.forEach(file => { console.log(`File Name: ${file.name}`); console.log(`File Contents (first 100 chars): ${file.contents.substring(0, 100)}`); }); }); container.addEventListener('dragover', dropHandler); container.addEventListener('drop', dropHandler); // Example of how to style the overlay (requires CSS for .drop-overlay, .box, .label) // document.head.insertAdjacentHTML('beforeend', ` // <style> // .drop-overlay { // position: absolute; // top: 0; left: 0; right: 0; bottom: 0; // background: rgba(0, 123, 255, 0.7); // color: white; // display: flex; // align-items: center; // justify-content: center; // font-size: 1.5em; // z-index: 1000; // } // .drop-overlay .box { // border: 2px dashed white; // padding: 20px; // } // </style> // `);
Debug
Known issues
breakingVersion 0.6.0 dropped UMD distribution and migrated the package to ES Modules (ESM). Projects using CommonJS `require()` statements will break.
fix
Update all module imports to use ES Module syntax (e.g., `import fileDrop from 'file-drops';`). Ensure your project's build tooling supports ESM.
affects: >=0.6.0
gotchaThe `dropHandler` function returned by `fileDrop` must be attached to both `dragover` and `drop` events to correctly handle the drag-and-drop interaction, including preventing default browser behavior.
fix
Ensure `element.addEventListener('dragover', dropHandler);` and `element.addEventListener('drop', dropHandler);` are both used on the target DOM element.
affects: >=0.1.0
gotchaThe package returns file contents as a string. For binary files or large files, this might not be the most efficient or desired format. Developers need to handle parsing or conversion themselves.
fix
If specific handling is needed for file types (e.g., `ArrayBuffer` for images), consider using a different library or converting the string content manually post-drop. The library focuses on simplicity and text-based content retrieval.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` to import `file-drops` in a project configured for ES Modules, or after upgrading to `file-drops` v0.6.0+.
fix
Change your import statement from `const fileDrop = require('file-drops');` to `import fileDrop from 'file-drops';`.
Uncaught TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only 1 present.
The `fileDrop` function's second argument (the callback) is missing or not a function, leading to an invalid event listener argument.
fix
Ensure `fileDrop` is called with both a label string and a callback function, e.g., `fileDrop('Drop a file', function(files) { /* ... */ });`.
Upgrade
Version history
0.7.0latest on npm
Audit
Dependencies
min-domrequiredInternal DOM manipulation utility, regularly updated as a runtime dependency.
Agent activity
2 hits · last 30 days
node
2
Resources
file-drops — npm install file-drops · libregistry