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

node-async-require

v2.0.0

Published

Transparently require() remote contents (node moudles) in Node.js !

Downloads

89

Readme

node-async-require

Build Status

Transparently require() remote contents (node moudles) in server-side Node.js !

npm install --save node-async-require

Concept

Fetch the remote contnets (node module) by http GET and require codes in Node.js.
I made up the file with .ajs extention.

  • Only for the require.extention to recognize the file.
  • The file contents is a node moudle that provides a remote url.
module.exports = {
   remoteUrl : "http://xxx.xxx.xxx/api/"
}
  • Node.js will fetch the contents by the remote url and require codes into local.

Highlight

  • Provid the PreParser config for parsing remote contents before Node.js requires it.
  • Privde the queryString config for fetching diffrent remote contents.
  • Provide the PreParser for react-templates!
  • How about isomorphic require() on client-side ? We got node-async-require-loader with webpack for it !

Basic Usage

Fetch the remote contnets (node module) by http GET and require codes in Node.js.
.ajs extention is only for require.extention to recognize the file.

  • Install this moudle
npm install --save node-async-require
  • Use directly in the js file.
require("node-async-require").install();
require("./remote-contents.ajs");//You can require any filename you want with .ajs extention

You may wondering what '.ajs' file is. The file contents of the .ajs file is a node moudle providing a remote url. The remote url that provides contents(node moudle). The .ajs extention is only for the loader to recognize the file.

Example of Basic Usage

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
The follwoing is the example of it.

remote-contents.ajs

module.exports = {
   remoteUrl : "https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/"
}
Step 2. Require the file like this

Require the files in this way.

require("node-async-require").install();
require("./remote-contents.ajs");

Then Node.js will fetch the remote contents by the url that .ajs file provides. The remote contents is as following.

module.exports=function(){ console.log("Hello World From Web"); }

The contents is a node module. It will be required to Node.js.

Usage with queryString

In some cases, the fixed remote url is not good. You may need to add queryString to fetch diffrent remote contents (node moudle).

Example with queryString

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
The follwoing is the example of it.

remote-contents.ajs

module.exports = {
   remoteUrl : "https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/"
}
Step 2. Require the file like this

Require the files in this way. Pass a parameter queryString to it.

require("node-async-require").install({
	queryString:"en" //pass a parameter to it.
});
require("./remote-contents.ajs");

The url you are going to request will append the queryString value.

So the actual url will be :

https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/en

Then Node.js will fetch the remote contents by the new url. The remote contents is as following.

module.export=function(){ console.log("Hello USA From Web"); }

The contents is a node module. It will be required to Node.js.

Usage with preParser

In some cases, the remote contents you fetch may not be a pure node moudle. You need a parser to do some stuffs before Node.js requires it. So you can set up a preParser for the remote contents.

Example with preParser

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
The follwoing is the example of it.

remote-contents.ajs

```js
module.exports = {
   remoteUrl : "https://jaydenlin.github.io/fake-remote-contents-for-test/contents/pure-js/"
}
Step 2. Require the file like this

Require the files in this way. Pass a parameter preParser to it.

require("node-async-require").install({
	preParser:function(remoteRawContent){
		remoteRawContent = "module.exports=function(){ console.log('Replaceed by custom preParser!');}";
            return remoteRawContent;
	} //pass a parameter to it.
});
require("./remote-contents.ajs");

The original contents will be replaced by preParser. So the actual contents will be :

module.exports=function(){ console.log('Replaceed by custom preParser!');}

The contents is a node module. It will be required to Node.js.

Usage with preParser (React Templates)

We provide a preparser for pasing react-templates contents.
You can use it by setting the preParser:"rt"

require("node-async-require").install({
	preParser:"rt" //pass a parameter to it.
});
require("./remote-contents-using-react-template.ajs");

Example with preParser (React Templates)

Step 1. Provide an .ajs file

The file contents of the .ajs file is a node moudle providing a remote url.
The follwoing is the example of it.

remote-contents-using-react-template.ajs

module.exports = {
   remoteUrl : "https://jaydenlin.github.io/fake-remote-contents-for-test/contents/react-template/"
}
Step 2. Require the file like this

Require the files in this way. Pass a parameter preParser to it. And set the value to rt.

require("node-async-require").install({
	preParser:"rt" //pass a parameter to it.
});
require("./remote-contents-using-react-template.ajs");

The remote contents of the url is the react-template as following.

<div>
  <h3>Hello World Form Web</h3>
</div>

The react-template contents will be parsed to the pure node module. And, it will be required to Node.js.

Test

You can use the following command to run mocha tests.

npm run test