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

typescript-lib2

v1.0.9

Published

This is a demo show how to create library

Downloads

12

Readme

initialize this library

How To Create a Node.js Module

init the folder

# direct initialize the library with default options
# this will just create the file "package.json"
$ npm init -y

add codes

Add codes in the new file "index.js", here is the content:

class Color {
  constructor(name, code) {
    this.name = name;
    this.code = code;
  }
}

const allColors = [
  new Color('brightred', '#E74C3C'),
  new Color('soothingpurple', '#9B59B6'),
  new Color('skyblue', '#5DADE2'),
  new Color('leafygreen', '#48C9B0'),
  new Color('sunkissedyellow', '#F4D03F'),
  new Color('groovygray', '#D7DBDD'),
];

exports.getRandomColor = () => {
  return allColors[Math.floor(Math.random() * allColors.length)];
}

exports.allColors = allColors;

This will export a list "allColors" and a function "getRandomColor()"

test

You can test lib with the Node.js REPL (Read-Evaluate-Print-Loop)

# this will enter REPL mode
$ node

> colors = require('./index');
> colors.getRandomColor();

# leave interactive mode
> .exit

test with application

Create the application "typescript-app2" in the same way.

# same with lib init, no extra things to setup
$ npm init -y

# install "typescript-lib2" and save the dependency
$ npm install --save ../typescript-lib2

Then, add a new file "index.js" with the content.

const colors = require('typescript-lib2');

const chosenColor = colors.getRandomColor();
console.log(`You should use ${chosenColor.name} on your website. It's HTML code is ${chosenColor.code}`);

Run application script "index.js"

$ node index.js

a global link for applications

This lib can add to global link (inside the folder "/usr/local/lib/node_modules")

# enter lib root, and run this command to put this lib to a global link
# /usr/local/lib/node_modules/typescript-lib2 -> ../../../../Users/fred/Fred/workspace/nodejs/typescript-lib2
$ sudo npm link

# then use this command in "typescript-app2" folder
# this will create a folder link in "node_modules" folder without dependency in package.json
$ sudo npm link typescript-lib2 

# unlink
$ npm unlink typescript-lib2

publish lib to npm

Publishing scoped public packages

$ npm publish --access public

Support typescript

Writing a Node.js module in TypeScript

What is a tsconfig.json/document

How To Run TypeScript Scripts with ts-node

Creating .d.ts Files from .js files

npm vs npx

https://stackoverflow.com/a/52018825/1896825

NPM - Manages packages but doesn't make life easy executing any.

NPX - A tool for executing Node packages.

ts REPL

not fully support for this feature. to enter the ts REPL, use this command

$ npx ts-node

ts script

# ts compiler
$ npm install typescript --save-dev

# function package, skip this
$ npm install emojione @types/emojione --save

Then, add "tsconfig.json". Or use this to generate "tsconfig.json" with all available options.

$ ./node_modules/.bin/tsc --init

Compile ts file "enmoji.ts" and "enmoji.test.ts"

# *.d.ts and *.js file will be generated on dist folder.
$ npm run build

Support Rollup

Rollup Tutorial

How to Setup a TypeScript project using Rollup.js