npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@typicalninja21/urlrequire

v2.0.1

Published

Easy to use module to require scripts over urls

Downloads

1

Readme

urlRequire

Easy to use module to require scripts over urls

  • This does exactly what it says on the tin, it allows you to require scripts over urls
  • It is a very simple module, and it is very easy to use
  • It is not safe, and it is not meant to be
  • However if your dumb enough you can use this in production : )

Table of Contents

  1. Installation
  2. Usage
    1. requireAsync
    2. requireSync
    3. options
  3. Tests

Install

Main Package

Even though you use this to require scripts over url, you cant require the package itself (so install it please)
npm i @typicalninja21/urlrequire

Optional package

  • For requireSync (not recommended, use requireAsync when possible) support, you MUST to install the following package

sync-fetch

npm i sync-fetch

Usage

This package is written in typescript, so you can use it with typescript, and the below examples will be in typescript, however you can use it with javascript if you want to.

Few Notes before using
  • The package is mostly great for 1 file scripts that is not too big

  • if you want to use it for a script that uses a lot of files, you may first want to convert that script to use urlRequire methods (so you can load the files/dependencies it wants using an url to them) or use patchRequire option

  • If you pass a direct package name instead of a url, it will try to resolve that url to a unpkg one (https://unpkg.com/[package-name]). however this would only work for a package that only has 1 file and (possibly no dependencies or have dependencies that only have 1 file, and with patchRequire enabled)

async

  • This uses requireAsync

Requiring Npm package :: ms

You can use a direct link to raw source code of the package if you want (ex: https://unpkg.com/[email protected]/index.js)

Typescript
import { requireAsync } from '@typicalninja21/urlrequire';

requireAsync('ms').then((ms) => {
    console.log(`Loaded ms`, ms)
    console.log(ms(20000))
}).catch((err) => {
    console.log(`Error loading ms`, err)
})
Javascript
const { requireAsync } = require('@typicalninja21/urlrequire');

requireAsync('ms').then((ms) => {
    console.log(`Loaded ms`, ms)
    console.log(ms(20000))
}).catch((err) => {
    console.log(`Error loading ms`, err)
})

Requiring a custom-script

  • Script used is hosted on Github
Typescript
const url = 'https://raw.githubusercontent.com/typicalninja493/urlrequire/main/example/example.js'
import { requireAsync } from '@typicalninja21/urlrequire';

requireAsync(url).then((myCustomScript) => {
    console.log(`Loaded myCustomScript`, myCustomScript)
    // stuff provided by our custom script
    console.log('#add()', myCustomScript.add(1, 2, 3, 4, 5))
    console.log('#multiply()', myCustomScript.multiply(1, 2, 3, 4, 5));
    console.log('#divide()', myCustomScript.divide(1, 2, 3, 4, 5))
    console.log('#subtract()', myCustomScript.subtract(1, 2, 3, 4, 5))
    console.log('#addStrings()', myCustomScript.addStrings('This is', ' ', 'cool', '', 'ngl'))
}).catch((err) => {
    console.log(`Error loading myCustomScript`, err)
})
Javascript
const url = 'https://raw.githubusercontent.com/typicalninja493/urlrequire/main/example/example.js'
const { requireAsync } = require('@typicalninja21/urlrequire');


requireAsync(url).then((myCustomScript) => {
    console.log(`Loaded myCustomScript`, myCustomScript)
    // stuff provided by our custom script
    console.log('#add()', myCustomScript.add(1, 2, 3, 4, 5))
    console.log('#multiply()', myCustomScript.multiply(1, 2, 3, 4, 5));
    console.log('#divide()', myCustomScript.divide(1, 2, 3, 4, 5))
    console.log('#subtract()', myCustomScript.subtract(1, 2, 3, 4, 5))
    console.log('#addStrings()', myCustomScript.addStrings('This is', ' ', 'cool', '', 'ngl'))
}).catch((err) => {
    console.log(`Error loading myCustomScript`, err)
})

sync

Using requireSync is NOT recommended (if possible please use requireAsync), however if your are the guy who wants to get the native node.js require feel from this. you are free to use this. (no awaits. no promises. just requiring)

  • requireSync requires optional dependency sync-fetch

Requiring Npm package :: ms

You can use a direct link to raw source code of the package if you want (ex: https://unpkg.com/[email protected]/index.js)

Typescript
import { requireSync } from '@typicalninja21/urlrequire';
// its almost similar to normal require
const ms = requireSync('ms')
console.log(ms(20000))
Javascript
const { requireSync } = require('@typicalninja21/urlrequire');

// its almost similar to normal require
const ms = requireSync('ms')
console.log(ms(20000))

Requiring a custom-script

  • Script used is hosted on Github
Typescript
const url = 'https://raw.githubusercontent.com/typicalninja493/urlrequire/main/example/example.js'
import { requireSync } from '@typicalninja21/urlrequire';
const myCustomScript = requireSync(url)
    console.log('#add()', myCustomScript.add(1, 2, 3, 4, 5))
    console.log('#multiply()', myCustomScript.multiply(1, 2, 3, 4, 5));
    console.log('#divide()', myCustomScript.divide(1, 2, 3, 4, 5))
    console.log('#subtract()', myCustomScript.subtract(1, 2, 3, 4, 5))
    console.log('#addStrings()', myCustomScript.addStrings('This is', ' ', 'cool', '', 'ngl'))
Javascript
const url = 'https://raw.githubusercontent.com/typicalninja493/urlrequire/main/example/example.js'
const { requireSync } = require('@typicalninja21/urlrequire');

const myCustomScript = requireSync(url)
    console.log('#add()', myCustomScript.add(1, 2, 3, 4, 5))
    console.log('#multiply()', myCustomScript.multiply(1, 2, 3, 4, 5));
    console.log('#divide()', myCustomScript.divide(1, 2, 3, 4, 5))
    console.log('#subtract()', myCustomScript.subtract(1, 2, 3, 4, 5))
    console.log('#addStrings()', myCustomScript.addStrings('This is', ' ', 'cool', '', 'ngl'))

Options

Second argument for requireAsync and requireSync is options which is an object.

  • options.requestOptions - Is a Object (default: {}).for requireAsync it takes a requestConfig of axios. for requireSync it takes a {} that will be passed to sync-fetch, sync-fetch runs node-fetch under the hood please refer docs for node-fetch for the requestConfig.

  • options.patchRequire - Is a boolean (default: true), a function (takes requireAsync or requireSync). If set to true the package will attempt to replace the native require function with the one provided by the package (or if provided a function, that function) in the required script.if this is set to false, the native require will be used, if you want to use require methods by this package, we also add requireSync and requireAsync to the global context of the required script.

  • options.passOptions - Is a Boolean.only works with above options.patchRequire is enabled.If set to true the package will the pass the same options you provided to the script you required

Tests

Tests are found in the test folder

  • Mocha is required to run the tests

  • run the tests with below command

npm run test

License

This repository and the code inside it is licensed under the MIT License. Read LICENSE for more information.