Friday, 2 November 2018

How to create a VERY basic (and useless) Node Package

This is just the absolute basics of a node package so that you can install it via npm, whether it be a private or publicly accessible repo. See my follow up post about how to install a package that's in a private bitbucket repo.

Firstly, setup a basic package.json as follows:

    {
        "name": "tripwiretech-common",
        "version": "1.0.0",
        "description": "Tripwiretech common code",
        "main": "index.js",
        "author": "Tripwiretech",
        "license": "ISC",
        "scripts": {
            "start": "node index.js"
        }
    }


The name is one of the key properties in the package.json as it will represent your node package name. Any repo with a package.json can be published to npm. It is also recommended that your repo contains a readme.md in the root of the repo, this will be the documentation displayed on the npm website, if you choose to publish it there.

Next you will need some content exported in your index.js that other code can import and use. For the purposes of this example, I've kept it quite simple:

    module.exports = {
        Countries: ["Australia", "New Zealand", "South Africa"],
        Sports: ["Rugby", "Cricket"]
    }


Then you can import it and use it in your code. If you're struggling with more complicated node packages, then I recommend taking a look at one of the many existing and much more practical open source node packages that already exist on npm.

No comments:

Post a Comment