Path Loader
path-loader is a JavaScript utility library designed to provide a unified API for loading content from both file paths and URLs, operating seamlessly across Node.js and browser environments. It currently supports `http`/`https` loaders for remote resources (default in browsers and for URLs in Node.js) and a `file` loader specifically for Node.js. While the README mentions future plans for a pluggable infrastructure, the package has not seen active development since its last release, version 1.0.12, in August 2016. Its release cadence is effectively inactive. The library differentiates itself by abstracting away environment-specific data loading mechanisms (e.g., XMLHttpRequest/fetch in browsers, `fs` or `http` modules in Node.js) behind a single `load` interface, utilizing `superagent` for AJAX functionality and `native-promise-only` for Promise polyfilling.
Common errors
-
Error: The file loader is not supported in the browser environment.
cause Attempting to use `loader.load()` with a local file path (e.g., `./data.json` or `file:///path/to/data.txt`) in a browser environment.fixEnsure that `path-loader` is only used with `http:` or `https:` URLs when running in a browser. Local files must be served via a web server. -
TypeError: loader.load is not a function
cause This usually occurs if `path-loader` was not correctly `require`d, or if an older, incompatible version was installed that lacked the `load` method, or if trying to call `load` directly on the `path-loader` module instead of the returned `loader` object.fixVerify that `const loader = require('path-loader');` is used and then call `loader.load(path)`. Check `package.json` to ensure `path-loader` is listed as a dependency and `npm install` has been run. -
UnhandledPromiseRejectionWarning: (node:XXXXX) Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
cause The `loader.load()` method returns a Promise, and if an error occurs during loading (e.g., file not found, network error), the promise will reject without an attached `.catch()` handler.fixAlways attach a `.catch()` block to your `loader.load()` calls to handle potential errors gracefully. Example: `loader.load(path).then(res => ...).catch(err => console.error(err));`
Warnings
- gotcha The `file` loader, which is used by default in Node.js for local paths, is explicitly not supported in browser environments and will throw an error if attempted.
- breaking The package has been effectively abandoned since its last update in August 2016. This means it may not be compatible with newer Node.js versions, browser APIs, or modern JavaScript features. Its dependencies (`superagent`, `native-promise-only`) may also contain unpatched security vulnerabilities or have breaking changes in their own development, potentially leading to supply chain issues.
- deprecated The primary API pattern is CommonJS `require()`. While UMD builds are provided for browsers, modern module systems (ESM) are not natively supported, making integration into contemporary build pipelines less straightforward.
Install
-
npm install path-loader -
yarn add path-loader -
pnpm add path-loader
Imports
- loader
import loader from 'path-loader';
const loader = require('path-loader'); - pathLoader (global)
<!-- In HTML after script tag --> <script src="path-loader.js"></script> <script> pathLoader.load('https://example.com/data.json') .then(console.log) .catch(console.error); </script> - load
const load = require('path-loader').load;const { load } = require('path-loader');
Quickstart
const loader = require('path-loader');
// Example: Loading content from a remote URL
loader.load('https://jsonplaceholder.typicode.com/posts/1')
.then(function (res) {
console.log('Successfully loaded remote data:');
console.log(JSON.parse(res.text));
})
.catch(function (err) {
console.error('Error loading remote data:', err.message);
});
// Example: Loading a local file (Node.js only)
// This will likely fail if the file does not exist or permissions are incorrect.
// In a real application, replace 'package.json' with a valid local path.
if (typeof window === 'undefined') { // Check if running in Node.js
loader.load('./package.json')
.then(function (res) {
console.log('\nSuccessfully loaded local file:');
console.log(JSON.parse(res.text));
})
.catch(function (err) {
console.error('\nError loading local file:', err.message);
});
}