Path Loader

1.0.12 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates loading JSON content from a remote URL (works in browser and Node.js) and a local file (Node.js only) using the unified `loader.load` API.

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);
    });
}

view raw JSON →