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

nodejs-package-starter

v1.0.5

Published

This is a start kit that creates an ES Modules format library that runs on node.js.

Downloads

4

Readme

nodejs-package-starter

This is a start kit that creates an ES Modules format library that runs on node.js.

Create your library.

  1. Create project.

    mkdir yourLibrary && cd $_;
  2. Create project configuration file.

    Execute the following command.
    This will create package.json at the root of the project.

    npm init -y;

    Open package.json and edit as follows.

    ...
    "main": "src/yourLibrary.js",
    ...

    |Name|Value|Description| |--|--|--| |main|src/yourLibrary.js|The main field is a module ID that is the primary entry point to your program.|

  3. Install required packages.

    npm i -S esm && npm i -D jest @babel/preset-env;

    |Name|Description| |--|--| |esm|Necessary for executing modules in ES Modules format with node.js.| |jest|Jest is a library for testing JavaScript code.| |@babel/preset-env|Compile the code under test from the latest specification to ECMAScript 5 so that it can be tested with Jest|

  4. Create a library module.

    Create a directory to store source files.

    mkdir src;

    Submodule that calculates the subtraction.

    // src/add.js
    /**
     * Sum two values
     */
    export default function (a, b) {
      return a + b;
    }

    Submodule that calculates the addition.

    // src/sub.js
    /**
     * Diff two values
     */
    export default function (a, b) {
      return a - b;
    }

    Main module that imports multiple modules and exports a single library.

    // src/yourLibrary.js
    import add from './add';
    import sub from './sub';
    export {add, sub};
  5. Let's run the library on node.

    Run the following command.

    node -r esm -e "\
        import {add} from './src/yourLibrary';
        console.log('1+2=' + add(1,2));";# 1+2=3
  6. Setting up and running unit tests.

    Then, Create add.test.ts and sub.test.ts files in the src directory.
    This will contain our actual test.

    // src/add.test.js
    import {add} from './yourLibrary';
    test('add 1 + 2 to equal 3', () => {
      expect(add(1, 2)).toBe(3);
    });
    // src/sub.test.js
    import {sub} from './yourLibrary';
    test('sub 2 - 1 to equal -1', () => {
      expect(sub(1, 2)).toBe(-1);
    });

    Add .babelrc to the project root and add the following content.

    { "presets": ["@babel/preset-env"] }

    Open your package.json and add the following script.

    ...
    "scripts": {
      "test": "jest"
    ...

    Run the test.

    npm run test;

    Jest will print this message.
    You just successfully wrote your first test.

    PASS  src/add.test.js
    PASS  src/sub.test.js
    
    Test Suites: 2 passed, 2 total
    Tests:       2 passed, 2 total
    Snapshots:   0 total
    Time:        1.329s
    Ran all test suites.

How to publish a npm package

  1. Create an NPM user locally.
    When the command is executed, a '~/.npmrc' file is created and the entered information is stored.

    npm set init.author.name 'Your name';
    npm set init.author.email '[email protected]';
    npm set init.author.url 'https://your-url.com';
    npm set init.license 'MIT';
    npm set init.version '1.0.0';
  2. Create a user on npm.
    If the user is not registered yet, enter the new user information to be registered in npm.
    If an npm user has already been created, enter the user information and log in.

    npm adduser;
  3. Create a repository on GitHub and clone.

    git clone https://github.com/your-user/your-repository.git;
  4. Setting files to be excluded from publishing

    Create an .npmignore file at the root of the project.

    .npmignore

    Add node_modules and package-lock.json to .npmignore not to publish.

    node_modules/
    package-lock.json
  5. Create v1.0.0 tag on GitHub.

    git tag -a v1.0.0 -m 'My first version v1.0.0';
    git push origin v1.0.0;
  6. Publish to npm

    npm publish;

How to upgrade NPM packages

  1. Push program changes to github

    git commit -am 'Update something';
    git push;
  2. Increase version

    npm version patch -m "Update something";
  3. Create a new version tag on Github

    git push --tags;
  4. Publish to npm

    npm publish;

Try this library

  1. Create project.

    mkdir myapp && cd $_;
  2. Create project configuration file.

    npm init -y;
  3. Install this library and esm.

    npm i -S nodejs-package-starter esm;
  4. Create a test module.

    Add library execution test module to project root.
     

    touch app.js;

    Add content:

    /* app.js */
    import {add, sub} from 'nodejs-package-starter';
    console.log(`1+2=${add(1,2)}`);// 1+2=3
    console.log(`1-2=${sub(1,2)}`);// 1-2=-1
  5. Run the test module.

    node -r esm app;

License

MIT

Author