I am working on my first NPM module. I briefly worked with typescript before and a big problem was that for many modules there were no definition files available. So I thought it would be a good idea to write my module in typescript.
However, I can't find any information on the best way to do that. I found this related question "Can I write npm package in coffeescript?" where people suggest only publishing the javascript files. But in contrast to the coffeescript files, the typescript files might actually be useful if they are used within a typescript application.
Should I include Typescript files when publishing an NPM module, or should I only publish the javascript files and provide the generated .d.ts files to DefinitelyTyped?
Recommended way in 2018 with Typescript 2.x:
declaration: true
totsconfig.json
to generate typings.index.ts
package.json
, point to your generated typings. For example if youroutDir
isdist
, then add"types": "dist/index.d.ts"
to your package json..npmignore
to ignore unnecessary files (e.g. the source).npm publish
. Use semver specifications for updates (patch / bug fixnpm version patch
, non-breaking additionsnpm version minor
, breaking api changesnpm version major
)Since it got me a while to sift through all the outdated resources on this topic on the internet (like the one on this page...) I decided to wrap it up in how-to-write-a-typescript-library with an up-to-date working minimal example.
Here is a sample Node module written in TypeScript : https://github.com/basarat/ts-npm-module
Here is a sample TypeScript project that uses this sample module https://github.com/basarat/ts-npm-module-consume
Basically you need to :
commonjs
anddeclaration:true
.d.ts
fileAnd then
.d.ts
.Atom-TypeScript just provides a nice workflow around this : https://github.com/TypeStrong/atom-typescript#packagejson-support
At Lossless we created a one stop TypeScript dev tool for npm packages: https://gitzone.gitlab.io/npmts/
I mainly follow the suggestion by Varun Chatterji
But, I would like to show a complete example with unit testing and code coverage and publishing it into
npm
and importing them usingjavascript
ortypescript
This module is written using
typescript 2.2
and it is important to configure theprepublish
hook to compile the code usingtsc
before publish it to npmhttps://github.com/sweetim/haversine-position
https://www.npmjs.com/package/haversine-position
This is a more recent answer using TypeScript 1.8.10:
My project structure is:
I added the following in
.npmignore
to avoid including extraneous files and keep the bare minimum to have the package imported and working:My
.gitignore
has:My
package.json
has:Now I run:
npm pack
The resultant file (when unzipped) has the following structure:
Now I go to the project where I want to use this as a library and type:
npm install ./project-1.0.0.tgz
It successfully installs.
Now I create a file
index.ts
in my project where I just installed the npmimport Project = require("project");
Typing
Project.
gives me the Intellisense options which was the point of this whole exercise.Hope this helps someone else in using their TypeScript npm projects as internal libraries in their bigger projects.
PS: I believe that this approach of compiling projects to npm modules which can be used in other projects is reminiscent of the
.dll
in the.NET
world. I could well imagine projects being organised in a Solution in VS Code where each project produces a an npm package which can then be used in another project in the solution as a dependency.Since it took a fair amount of time for me to figure this out, I have posted it in case someone is stuck here.
I also posted it for a closed bug at: https://github.com/npm/npm/issues/11546
This example has been uploaded to Github: vchatterji/tsc-seed
You can use autodts to handle distributing and using
.d.ts
files from npm also without support from the Atom IDE.autodts generate
will bundle all your own.d.ts
files together for publishing on npm, andautodts link
handles references to other installed packages, which may not always be directly undernode_modules
in a larger project split into several subpackages.Both commands read their settings from
package.json
andtsconfig.json
in "convention over configuration" style.There's another answer on stackoverflow and a blog post with more details.