Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1495

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T08:42:09+00:00 2024-11-25T08:42:09+00:00

How to publish TypeScript package to NPM

  • 62k

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

  • react-use-throttle package on NPM
  • GitHub repo
  • CodeSandbox demo
  • Article about developing useThrottle hook
  • My personal website

javascriptreacttypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.