{"id":11529,"library":"path-loader","title":"Path Loader","description":"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.","status":"abandoned","version":"1.0.12","language":"javascript","source_language":"en","source_url":"https://github.com/whitlockjc/path-loader","tags":["javascript","json"],"install":[{"cmd":"npm install path-loader","lang":"bash","label":"npm"},{"cmd":"yarn add path-loader","lang":"bash","label":"yarn"},{"cmd":"pnpm add path-loader","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used to shim in Promises support for environments that lack them.","package":"native-promise-only","optional":false},{"reason":"Provides AJAX capabilities for both browser and Node.js environments.","package":"superagent","optional":false}],"imports":[{"note":"Primarily designed for CommonJS; direct ESM import not officially supported. For browser, it exposes a global `pathLoader`.","wrong":"import loader from 'path-loader';","symbol":"loader","correct":"const loader = require('path-loader');"},{"note":"When used directly in a browser via a `<script>` tag, the utility is exposed as a global `pathLoader` object.","symbol":"pathLoader (global)","correct":"<!-- In HTML after script tag -->\n<script src=\"path-loader.js\"></script>\n<script>\n  pathLoader.load('https://example.com/data.json')\n    .then(console.log)\n    .catch(console.error);\n</script>"},{"note":"The `loader` object returned by `require('path-loader')` has a `load` method. Destructuring can be used.","wrong":"const load = require('path-loader').load;","symbol":"load","correct":"const { load } = require('path-loader');"}],"quickstart":{"code":"const loader = require('path-loader');\n\n// Example: Loading content from a remote URL\nloader.load('https://jsonplaceholder.typicode.com/posts/1')\n  .then(function (res) {\n    console.log('Successfully loaded remote data:');\n    console.log(JSON.parse(res.text));\n  })\n  .catch(function (err) {\n    console.error('Error loading remote data:', err.message);\n  });\n\n// Example: Loading a local file (Node.js only)\n// This will likely fail if the file does not exist or permissions are incorrect.\n// In a real application, replace 'package.json' with a valid local path.\nif (typeof window === 'undefined') { // Check if running in Node.js\n  loader.load('./package.json')\n    .then(function (res) {\n      console.log('\\nSuccessfully loaded local file:');\n      console.log(JSON.parse(res.text));\n    })\n    .catch(function (err) {\n      console.error('\\nError loading local file:', err.message);\n    });\n}","lang":"javascript","description":"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."},"warnings":[{"fix":"In browser environments, only use `http:` or `https:` URLs. To load local files, consider a web server to serve them via HTTP or alternative browser-specific file APIs.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For new projects, consider modern alternatives like `node-fetch` (Node.js) or the native `fetch` API (browser) for HTTP requests, and `fs.promises` (Node.js) for file system operations. If you must use `path-loader`, thorough security audits and compatibility testing are essential.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"For Node.js ESM projects, you might need a wrapper or dynamic import (`import('path-loader')`) if direct `require` is problematic. For browser ESM, a bundler like Webpack or Rollup would be necessary, but consider migrating to modern `fetch` or a actively maintained HTTP client library.","message":"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.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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.","error":"Error: The file loader is not supported in the browser environment."},{"fix":"Verify 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.","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.","error":"TypeError: loader.load is not a function"},{"fix":"Always 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));`","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.","error":"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()."}],"ecosystem":"npm"}