<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matthew Morton on Medium]]></title>
        <description><![CDATA[Stories by Matthew Morton on Medium]]></description>
        <link>https://medium.com/@mmortondeveloper?source=rss-88a5445f05af------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ySbTgfet8g9fC25zo3uNzA.jpeg</url>
            <title>Stories by Matthew Morton on Medium</title>
            <link>https://medium.com/@mmortondeveloper?source=rss-88a5445f05af------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 12:31:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mmortondeveloper/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[npm: Publishing a Package]]></title>
            <link>https://medium.com/@mmortondeveloper/how-to-build-and-publish-an-npm-package-with-typescript-the-right-way-916d108d0d19?source=rss-88a5445f05af------2</link>
            <guid isPermaLink="false">https://medium.com/p/916d108d0d19</guid>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript-library]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Matthew Morton]]></dc:creator>
            <pubDate>Tue, 16 Jun 2026 02:59:13 GMT</pubDate>
            <atom:updated>2026-11-30T23:30:11.739Z</atom:updated>
            <content:encoded><![CDATA[<h4>A personal reference for when I inevitably forget how to do this well (note: I’m updating this guide to use tsdown, currently WIP; Still useful, but use common sense, consider yourself warned. I’ll republish this ASAP with my schedule)</h4><figure><img alt="A simple icon of a vintage computer, on a black banner background." src="https://cdn-images-1.medium.com/max/736/1*kRv1Tqi3A6jSckiFttpbaA.jpeg" /><figcaption>Computer, because this is computer programming, baby. At a high level, admittedly… maybe I’ll rewrite this in x86-64 ASM to finally prove that I know what I’m doing. Unfortunately Hyde’s book doesn’t cover apple silicon. Drat!</figcaption></figure><h3>Introduction</h3><p>In the six months since I first published this article, tsup was deprecated. So now I must return and rewrite my package with tsdown; my castle is built on pillars of sand; the creeping hand of time comes for us all.</p><p>I’m Matt, a CompSci student in the great year of 2026 (agreed upon as a very GOOD year for CompSci students, generally). I program because I like it, I think it’s neat. I’ve learned basically everything by the kind hand of the free software community, and so I decided to open source some stuff of my own, now that I know enough about anything to not produce foul slopware.</p><p>I built a social media platform through an open beta last year (before I ran out of runway), a specialty coffeehouse discovery platform called Roastly. A big part of this was a map UI, and naturally a ton of geographic data. I found a way to geocode for actually zero dollars, which was extremely wizard and righteous, and I released the package. It is at the expense of rooftop-accuracy, however. Tradeoffs!</p><h3><strong>What This Article Covers:</strong></h3><ul><li>Bundling for ESM (ECMAScript Module) and CJS (CommonJS) with <a href="https://www.npmjs.com/package/tsup">tsup</a> <strong>(edit: as of </strong><a href="https://github.com/egoist/tsup/commit/8b6907df5d944e5e4a639a297875a35818ba9fa5"><strong>Aug 13 2026 tsup is deprecated</strong></a><strong>, this guide will use </strong><a href="https://www.npmjs.com/package/tsdown"><strong>tsdown</strong></a><strong>)</strong></li><li>Writing a package.json that work with modern toolchains</li><li>Auto-generating API docs</li><li>Automating releases with GitHub Actions</li><li>Publishing to npm</li></ul><p>The goal is a publishable, automatically-documented, CI-backed TypeScript package, with minimal complexity.</p><h3><strong>Why TypeScript?</strong></h3><p>Do you want to guess what datatype everything should return? Perhaps you hate yourself, and everyone else; in which case, that is a good choice.</p><p>We see with TypeScript that statically-typed languages are an emergent phenomenon in the industry: JS was frustrating enough that TypeScript was brought down from Olympus and given to mankind (at the expense of someone’s liver, maybe), and we see the same happen in Python 3.5 with <a href="https://docs.python.org/3/library/typing.html">typing</a>.</p><p>If you’re a die-hard dynamically-typed language fan (first, <em>why</em>?) then I ask you:</p><p>Why does typing keep appearing in your dynamically-typed languages?</p><h4>A Short Rant on the Cost of Dynamic Typing</h4><p>In a word, the goal of good code (of any kind, very generally) is to be easily understood by someone who has never seen it (including the author, after spending so much time away they <em>might as well</em> have never seen it).</p><p>Dynamically typed languages make this harder, because there are simply more steps involved in figuring out the context of variables and functions. So, the small amount of time saved writing the code is paid for exponentially <em>every time the code is referenced.</em></p><p><a href="https://youtu.be/8ncQrGuunHY?si=_LTUNAsVXY-RKgTw">See this very funny and well-put video by Internet of Bugs on the topic of code maintainability</a>.</p><p>This is my personal view of the argument; feel free to discuss this in the comments if you think I am wrong. I have been wrong before, and I will likely at some point be wrong again. However, I will definitely at all points be Matt.</p><p>Let’s get down to business.</p><h3>Step 1: Project Structure</h3><p>Separate your source from your compiled output, and keep your configuration at the root where tools and other people expect it.</p><pre>my-package/<br>├── src/<br>│   ├── index.ts       # Public API — the entry point, filled with exports<br>│   └── lib/           # Internal modules<br>├── dist/              # Generated by your bundler — never edit manually<br>├── docs/              # Generated documentation<br>├── tdown.config.ts<br>├── tsconfig.json<br>├── package.json<br>└── README.md</pre><h4>Setting up API surface:</h4><p>src/index.ts is your API surface, or in other words, the functions/variables/whatever which the library exposes to end users. We could have a whole talk about having <a href="https://www.janmeppe.com/blog/a-philosophy-of-software-design-john-ousterhout/#6-deep-modules-are-less-complex-than-shallow-modules">deep modules</a>, but that’s another article. Essentially, the ideal API surface is relatively small, and abstracts complicated implementation away from the end user.</p><p>For example, I’ll use the library I published, <a href="https://www.npmjs.com/package/lance-ts">lance-ts</a>. It’s a client for the <a href="https://geocoding.geo.census.gov/geocoder/">US Census Bureau Geocoding API</a>, which is a free tool for geocoding in the US (it’s not rooftop-accurate, but it is <em>free</em>). LANCE’s src/index.ts file looks like this:</p><pre>/**<br> * @module lance-ts<br> * @author [Matthew Morton](https://github.com/matt-the-thew)<br> */<br>export * from &quot;./one-line-address.js&quot;;<br>export * from &quot;./batch-address.js&quot;;</pre><p><a href="https://github.com/matt-the-thew/lance-ts">Here’s a link to the repo</a>, if you want to check out the rest of the code. A great de-slopification has commenced since the origin release, spurred on from the deprecation of tsup.</p><p>Both src/one-line-address.js and src/batch-address.js export functionality, using file-level export statements. The logic in the index.ts file makes the following code work:</p><pre>import * as lance from &quot;lance-ts&quot; // grabs all exports from src/index.ts</pre><p>This is why setting up this file is extremely important, and changes to it must be <strong>announced </strong>and <strong>hollered about. </strong>Anything that changes your API surface is a major, breaking change, so do it intentionally if you must do it at all.</p><h3>Step 2: tsconfig.json</h3><p>Your TypeScript config for a library is slightly different from one for an app. You want strict mode on, and you want to emit declaration files so users can import type information (<em>that’s the whole point!</em>).</p><pre>{<br>  &quot;compilerOptions&quot;: {<br>    &quot;target&quot;: &quot;ES2020&quot;,<br>    &quot;module&quot;: &quot;ESNext&quot;,<br>    &quot;moduleResolution&quot;: &quot;bundler&quot;,<br>    &quot;strict&quot;: true,<br>    &quot;declaration&quot;: true,<br>    &quot;declarationMap&quot;: true,<br>    &quot;sourceMap&quot;: true,<br>    &quot;outDir&quot;: &quot;./dist&quot;<br>  },<br>  &quot;include&quot;: [&quot;src&quot;]<br>}</pre><p>declarationMap is easy to skip, but worth not skipping. It lets IDEs &quot;go to definition&quot; commands jump to your actual source instead of the compiled .d.ts file. I’ve been using <a href="https://www.google.com/aclk?sa=L&amp;pf=1&amp;ai=DChsSEwj8iIeN14qVAxWpMUQIHb2LGVYYACICCAEQABoCZHo&amp;co=1&amp;ase=2&amp;gclid=CjwKCAjwxb7RBhA5EiwAQ-AAdC5LJnCud6r3-ckyT3QRYLGppyOL4pPXQsZzhzEGj_r8TOo2zv2ZXhoCNwMQAvD_BwE&amp;ei=h7AwarmwIajQkPIPhbudmQI&amp;cid=CAAS0wHkaMFY-pBpBGtyIJ-iEF3w68nbMqRvMDS7KfDast7g6LzueFv17-isp39_yREBmddy796iCxmxFflS8QmRmYiUwMlK9itKY2IScZNbu2J3wDmxVVq8-bw3_oXBeqkd3sOruJhPN5-Cy2o11GowLgZuX6THvYhVfphaG8mJkAcHgDzpwpF08dYpoLkehOORKGU9IPfMQ_bxgYF823bJN6UPuSjfGOND09RqOfVQumYlvzyB8_oY8n-wPZQZrSprrcVd5hfG1MwpeCTMs9NXPQf0y4y2&amp;cce=2&amp;category=acrcp_v1_32&amp;sig=AOD64_24EQ7NfIZfH6o8QHZf8fvjMC5wzA&amp;q&amp;sqi=2&amp;nis=4&amp;adurl=https://zed.dev/?utm_source%3Dgoogle%26utm_medium%3Dcpc%26utm_campaign%3Dbrand%26utm_term%3Dzed%2520editor%26gad_source%3D1%26gad_campaignid%3D23608930243%26gbraid%3D0AAAABC6KK9VjzPFCA1HTL9xfWHAIzWodt%26gclid%3DCjwKCAjwxb7RBhA5EiwAQ-AAdC5LJnCud6r3-ckyT3QRYLGppyOL4pPXQsZzhzEGj_r8TOo2zv2ZXhoCNwMQAvD_BwE&amp;ved=2ahUKEwj5j4GN14qVAxUoKEQIHYVdJyMQ0Qx6BAgNEAE">Zed</a> recently, with vim motions (of course), and while that may frustrate and anger vi purists, I use “go to definition” and “go to implementation” extremely often in my workflow. A small thing like this makes your library noticeably better to use. It’s all about User eXperience! Keep that UX <em>smooth</em>.</p><h3>Step 3: Bundling with TSUP</h3><p>TSUP is a great tool for bundling TypeScript libraries, in my opinion. It wraps esbuild, which makes it fast, and it handles the ESM/CJS dual-format problem that trips up a lot of first-time publishers. If you don’t know what those letters mean, its <em>ECMAScript Module, </em>and <em>Common JS</em>, the former being the modern standard, and the latter a legacy hold-over. ES Modules give a lot of benefits, but if you want to help the most people, bundling for legacy JS projects is a really considerate, good idea, and very easily implemented.</p><h4>Short TSUP How-To:</h4><pre>npm install --save-dev tsup</pre><p>Then create tsup.config.ts:</p><pre>import { defineConfig } from &quot;tsup&quot;;<br><br>export default defineConfig({<br>  entry: [&quot;src/index.ts&quot;],<br>  format: [&quot;esm&quot;, &quot;cjs&quot;],<br>  dts: true,<br>  sourcemap: true,<br>  clean: true,<br>});</pre><p>Two formats matters because the Node.js ecosystem is still mid-transition. Some consumers use require() (CommonJS), while others use import (ES Module). Publishing both means you&#39;re helping the most people. The dts: true flag generates your declaration files alongside the bundles — no separate tsc step needed.</p><h3>Step 4: package.json — The Fields That Matter</h3><p>package.json is the <strong>beating heart of your project</strong>. It’s deceptive because it just feels like a lot of JSON that doesn’t do anything. It is not so. A well-configured package.json is what makes your package work correctly across different environments and toolchains, increases your contributors’ DX, and allows users to find you when searching the npm registry.</p><h4>Minimum Viable package.json example:</h4><pre>{<br>  &quot;name&quot;: &quot;your-package-name&quot;,<br>  &quot;version&quot;: &quot;0.1.0&quot;,<br>  &quot;description&quot;: &quot;A concise, accurate description for npm search&quot;,<br>  &quot;keywords&quot;: [&quot;typescript&quot;, &quot;your-domain&quot;, &quot;relevant-term&quot;],<br>  &quot;main&quot;: &quot;./dist/index.js&quot;,<br>  &quot;module&quot;: &quot;./dist/index.mjs&quot;,<br>  &quot;types&quot;: &quot;./dist/index.d.ts&quot;,<br>  &quot;exports&quot;: {<br>    &quot;.&quot;: {<br>      &quot;import&quot;: &quot;./dist/index.mjs&quot;,<br>      &quot;require&quot;: &quot;./dist/index.js&quot;,<br>      &quot;types&quot;: &quot;./dist/index.d.ts&quot;<br>    }<br>  },<br>  &quot;files&quot;: [&quot;dist&quot;],<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;some-test-runner&quot;,<br>    &quot;build&quot;: &quot;tsup&quot;,<br>    &quot;prepublishOnly&quot;: &quot;npm run build&quot;<br>  }<br>}</pre><p>A few things worth understanding here:</p><p><strong>exports</strong> is the modern way to define your package&#39;s public API at the file-system level. It tells bundlers and runtimes exactly which file to use for which import style. Without it, you&#39;re relying on older resolution behavior that doesn&#39;t always do what you expect. This is where you configure for legacy and modern ES paradigms, which I mentioned earlier.</p><p><strong>files</strong> controls what actually gets included in the published package. Without it, npm publishes everything — your source, your tests, your config files. [&quot;dist&quot;] keeps the package clean and small. The ./dist/ directory is generated for you, by tsup, and could theoretically be named anything. “Dist” is merely a good convention to follow, and I recommend you do so.</p><p><strong>prepublishOnly</strong> runs your tsup build automatically before every publish. This is a cute little package.json script-chaining example. This helps you by making it impossible to accidentally ship stale compiled output. That’s always embarassing, especially because npm <strong>requires</strong> a version bump with each release. To avoid 1.1.567, I vehemently recommend including this in your project.</p><h3>Step 5: Generating Documentation with TypeDoc</h3><p>Strong documentation is a hallmark of a good Software Engineer, and a love-letter to your users. A library without it is a library people don’t use. <a href="https://typedoc.org/">TypeDoc</a> generates API docs directly from your TypeScript source and <a href="https://jsdoc.app/">JSDoc comments</a>, which means documentation and code stay in sync with inline explanations. TypeDoc is the more recent cousin of JSDoc, which has been around much longer. It simply uses the syntax of JSDoc, and has more functionality, for modern TypeScript.</p><h4>A Simple TypeDoc How-To:</h4><pre>npm install --save-dev typedoc</pre><p>Add a typedoc.json to your project root:</p><pre>{<br>  &quot;entryPoints&quot;: [&quot;src/index.ts&quot;],<br>  &quot;out&quot;: &quot;docs&quot;,<br>  &quot;plugin&quot;: [<br>    /* <br>    I recommend looking up the many, many useful plugins<br>    which exist to make your documentation both easy to <br>    browse, and pleasing to the eyeballs...<br>    */<br>  ],<br>  &quot;readme&quot;: &quot;README.md&quot;<br>}</pre><p>Then add a script:</p><pre>&quot;docs&quot;: &quot;typedoc&quot;</pre><p>How to write JSdoc → TypeDoc comments is a whole other article. In a word, however, TypeDoc reads /** */ blocks and turns them into structured documentation.</p><h3>Step 6: Publishing to npm</h3><p>If you don’t have one, create an account at <a href="https://www.npmjs.com/">npmjs.com</a>. Then authenticate locally in your cli:</p><pre>npm login</pre><p>This will prompt you to open the npm website and authenticate.</p><p>Once this is done, but before you publish, it’s usually smart to check what you’re actually shipping:</p><pre>npm pack --dry-run<br>// then... <br>npm publish --dry-run</pre><p>This lists every file that will be included in the package without actually publishing. You want to see your dist/ contents, and nothing else. The second command runs all the logic to publish to npm’s registry, without actually pushing any code. A good, final E2E test, and intelligent to do.</p><p>If all this succeeds, <strong>congratulations.</strong> Upload the package for real, with:</p><pre>npm publish</pre><p>For scoped packages (e.g. @yourname/package), add --access public, if it&#39;s open source:</p><pre>npm publish --access public</pre><p>That’s it. Your package is live, and you are officially an <strong>open-source developer</strong>.</p><h3>A Short Word on Versioning:</h3><p>npm packages live and die by <a href="https://semver.org/">Semantic Versioning</a>. This is a widely accepted standard for informing your users of patches, minor changes, and breaking changes. The rules are simple and detailed below:</p><ul><li><strong>Patch</strong> (0.1.0 → 0.1.1): bug fixes, nothing changes for consumers</li><li><strong>Minor</strong> (0.1.0 → 0.2.0): new features, fully backward compatible</li><li><strong>Major</strong> (0.1.0 → 1.0.0): breaking changes (i.e. API public surface shift or equivalent)</li></ul><p>If you must break the public API, <em>and sometimes you must</em>, that’s a major version bump. Always! Developers lock projects to your version because they trust you to keep that code the same. Breaking it silently is bad for business.</p><h3>A Short Reflection on Open Sourcing:</h3><p>Open-sourcing something is a great feeling. When you work hard on a project, it’s extremely rewarding to put it into the world and have it help people.</p><p>When I released LANCE, I got some very nice feedback in some geography subreddits, thanking me for making it. It was a strange and wonderful feeling to have people I don’t know, across the world, reading my code and appreciating my work. I was immediately hooked.</p><p>The moment a project goes public, it stops being yours in the literal sense. It’s merely a tool, good code that belongs to whoever finds it: someone building a delivery app in Berlin, a student working on their first mapping project, a team that runs npm install and never once wonders who wrote it. Your reward is becoming a better engineer, by having to think harder about building a tool that strangers will understand.</p><p>That’s a great way to help a lot of people, very efficiently, and frankly look handsome, beautiful, and dashing while doing it. Cheers, and Happy Hacking.</p><p>Morton.</p><h3>Resources</h3><ul><li><a href="https://tsup.egoist.dev/">TSUP Documentation</a></li><li><a href="https://typedoc.org/">TypeDoc Documentation</a></li><li><a href="https://docs.npmjs.com/cli/v10/configuring-npm/package-json">npm Docs: package.json</a></li><li><a href="https://semver.org/">Semantic Versioning</a></li><li><a href="https://www.npmjs.com/package/lance-ts">lance-ts on npm</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=916d108d0d19" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>