How to publish TypeScript package to NPM

loonywizard

Vladimir Nikitin

Posted on April 20, 2021

How to publish TypeScript package to NPM

In this article I'm going to cover a process of publishing TypeScript package with external dependencies to NPM

Write some code

The package we're going to publish is React.js custom hook for throttling values: react-use-throttle. I've already written article about developing this hook and now we're going to publish it to NPM

First things first, we need to write code for the package. I've put my code to src/index.ts file

tsconfig.json

In order to develop with TypeScript we need to add tsconfig to our repository. My config looks like this:

{
  "include": ["./src/**/*"],
  "compilerOptions": {
    "strict": true,
    "declaration": true, // generates declaration files
    "esModuleInterop": true
  }
}
Enter fullscreen mode Exit fullscreen mode

To learn more about different options please look at TSConfig Reference

Set up Rollup

Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application

Rollup allows developers easily compile code into different JavaScript module systems such as ESModules, UMD, AMD or CommonJS. There's a great article covering major differences between them

This is my rollup.config.js file, it exports an array of objects, where each object defines how Rollup should build our code in specified format. Here we're building code for ES and UMD modules, because there're most common nowadays. Each bundle has TypeScript and Babel plugins, and UMD bundle also has terser plugin for code minification

import typescript from 'rollup-plugin-typescript2'
import babel from '@rollup/plugin-babel'
import { terser } from 'rollup-plugin-terser'

export default [
  // ES Modules
  {
    input: 'src/useThrottle.ts',
    output: {
      file: 'dist/index.es.js', format: 'es',
    },
    plugins: [
      typescript(),
      babel({ extensions: ['.ts'] }),
    ],
  },

  // UMD
  {
    input: 'src/useThrottle.ts',
    output: {
      file: 'dist/index.umd.min.js',
      format: 'umd',
      name: 'reactUseThrottle',
      indent: false,
    },
    plugins: [
      typescript(),
      babel({ extensions: ['.ts'], exclude: 'node_modules/**' }),
      terser(),
    ],
  },
]
Enter fullscreen mode Exit fullscreen mode

To learn more about Rollup configuration please look at Rollup quick start guide

Build code and publish package to NPM

We need to specify the following fields in package.json file

"name": "react-use-throttle",
"version": "0.0.1",
"main": "dist/index.umd.min.js",
"module": "dist/index.es.js",
"types": "dist/useThrottle.d.ts",
"files": ["dist"]
Enter fullscreen mode Exit fullscreen mode

name and version together identify package completely unique
main is the primary entry point to our package
module is the entry point for ESModules
types is the entry point for TypeScript type declarations
files is an array of patterns that describes the entries to be included when your package is installed as a dependency

Learn more about different fields in package.json file: Package.json docs

Also, we need to specify react as peerDependency, because it will not be added to final build

"peerDependencies": {
  "react": "^16.8.0  || ^17.0.0"
}
Enter fullscreen mode Exit fullscreen mode

To build code with rollup we need to run the following command:

rollup -c
Enter fullscreen mode Exit fullscreen mode

It will build our package based on rules we defined in rollup.config.js. Code will be generated to dist folder

Now we're ready to publish our package, to do this we need to run the following commands:

npm login # this will ask you for your NPM login and password
npm publish
Enter fullscreen mode Exit fullscreen mode

Package was successfully published to NPM 🎉

Alt Text

Links

💖 💪 🙅 🚩
loonywizard
Vladimir Nikitin

Posted on April 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related