NPM Packages with TypeScript

Samuele
5 min readSep 26, 2021

The more I play with the programming, the more I realize that I am often rewriting the same code. I therefore decided to upload some functions to NPM. But how can I create an NPM package using TypeScript? Well, I had to put together a few guides and do some tests. These are the steps I followed.

Initialize the package and install TypeScript

I create the package.json file with default values:

npm init -y

Then I add the node_modules folder among the paths to ignore in .gitignore.

I install TypeScript:

npm install --save-dev typescript

I configure the tsconfig.json file:

{
"compilerOptions": {
"outDir": "./lib",
"module": "ES2020", // or commonjs
"target": "ES2019",
"lib": [
"ES2019",
"DOM",
"ES2020"
],
"declaration": true,
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}

I create a src folder in which to put the starting code. Then I add the index.ts file:

const myCustomFunction = (name: string):string => `Hello ${name}`;
function ciao(name: string): string {
return `Ciao ${name}`;
}

export {

--

--

Samuele

I'm a hobby programmer, experimenting with Svelte, Javascript, Construct 3 and magic tools