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 alre…


This content originally appeared on DEV Community and was authored by Vladimir Nikitin

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
  }
}

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(),
    ],
  },
]

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"]

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"
}

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

rollup -c

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

Package was successfully published to NPM ?

Alt Text

Links


This content originally appeared on DEV Community and was authored by Vladimir Nikitin


Print Share Comment Cite Upload Translate Updates
APA

Vladimir Nikitin | Sciencx (2021-04-20T13:33:39+00:00) How to publish TypeScript package to NPM. Retrieved from https://www.scien.cx/2021/04/20/how-to-publish-typescript-package-to-npm/

MLA
" » How to publish TypeScript package to NPM." Vladimir Nikitin | Sciencx - Tuesday April 20, 2021, https://www.scien.cx/2021/04/20/how-to-publish-typescript-package-to-npm/
HARVARD
Vladimir Nikitin | Sciencx Tuesday April 20, 2021 » How to publish TypeScript package to NPM., viewed ,<https://www.scien.cx/2021/04/20/how-to-publish-typescript-package-to-npm/>
VANCOUVER
Vladimir Nikitin | Sciencx - » How to publish TypeScript package to NPM. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/20/how-to-publish-typescript-package-to-npm/
CHICAGO
" » How to publish TypeScript package to NPM." Vladimir Nikitin | Sciencx - Accessed . https://www.scien.cx/2021/04/20/how-to-publish-typescript-package-to-npm/
IEEE
" » How to publish TypeScript package to NPM." Vladimir Nikitin | Sciencx [Online]. Available: https://www.scien.cx/2021/04/20/how-to-publish-typescript-package-to-npm/. [Accessed: ]
rf:citation
» How to publish TypeScript package to NPM | Vladimir Nikitin | Sciencx | https://www.scien.cx/2021/04/20/how-to-publish-typescript-package-to-npm/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.