modules are node modules that allow you to easily reuse a chain of transforms and even publish it for others to use. In this article we'll drill down on ways to write and use modules.
Let's start with absolute basics, how modules are used based on scramjet-hello-module:
A module needs to be installed with this simple shell command:
_10npm i scramjet-hello-module
Then we use it by passing it to the
use method of any Scramjet stream.
Modules will work also on any standard node.js stream:
_10import hello from "scramjet-hello-module";_10_10hello(myStream, argument1); // returns a new stream
You can also use modules that are not published in NPM or in node_modules:
_10stream.use("./my-parse-module"); // will look for a file in the same directory as the file where it's used._10stream.use("/path/to/some/module"); // will look for a certain path_10stream.use("my-module/submodule"); // will in node_modules/my-module/submodule.js
Finally sometimes you may want to use multiple modules:
_10DataStream.from(someSource).use("./find-names").use("scramjet-hello-module", argument1);_10// ... and so on.
Developing a module
As an example we'll use a module that converts the stream entries to numbers and filters out non-numbers.
Basics first - it's all in a simple file:
_10module.exports = require("scramjet").createTransformModule(stream =>_10stream.map(x => parseFloat(x)).filter(x => isNaN(x))_10);
If you save it as
./only-numbers you can use it in your code to filter out non-numbers from any of your streams in the specific project you put your file in. That's it, you're done, use it and have fun...
But what if you need to use this module in another project?
Fair question. Why not make an actual node module out of it - what you need is a
package.json and somewhere to store it - a git repo works well.
Create a directory and put your
_12# create your local git repo_12$ git init ._12_12# add your remote (create it on github or wherever you like)_12$ git remote add origin [email protected]:yourname/yourmodule.git_12_12# Answer some questions here and you'll get a nice package json._12# make sure that the answer to "entry point: (index.js)" is "only-numbers.js"_12$ npm init_12_12# Add commit and push_12$ git add . && git commit -m "Initial commit" && git push
You'll end up with a module in git that should have the following files:
_10\_10|- .git/ # this is where your git files sit_10|- only-numbers.js # this is your actual scramjet module_10|- package.json # this is where your git files sit
Can I fork something to develop my module quicker?
Sure! Here's a repo made especially for you:
What if my module is good for public?
I congratulate you sir! Now this is fairly simple:
But please consider doing a couple things first:
- Read the NPM guide on publishing packages - this will make you package stand out.
- Consider adding some checks like
Snykso that you keep your package up to date.