- Setup
- Build and set main, types in package.json
- .npmignore
- Pack
- Installing a local package
- Next steps
npm init -y
npm install typescript @types/node --save-dev
npx tsc --init
Add a build script to your package.json
.
{
"scripts": {
"build": "npx tsc"
},
}
Set up your src
directory.
Set up your tsconfig.json
file, at the very least:
- target
- module
- rootDir
- outDir
In addition set "declaration": true
.
Tip: If you are using an external package, consider setting all references to that package in one file, then export/import the function that uses that package as needed.
Once your package is ready to be shared in another project, build it, then set the output index.js
as your main file. In addition, add the typescript types file (generated from the "declaration": true
option) in your packahe.json
.
{
"main": "dist/index.js",
"types": "dist/index.d.ts",
}
Use this file in the root directory to identify which files should be excluded from the package:
src
tsconfig.json
notes.txt
node_modules
Run the command to create a tarball:
npm pack
It will create a -1.0.0.tgz
file of your package in the root directory.
Let's say our package is in the parent directory of our project:
npm install ../my-package-1.0.0.tgz
If you want to publish your package on npm, here's some guides: