Node.js packages are modules of reusable code that can be easily shared and integrated into other projects. These packages can be published and downloaded from the Node Package Manager (npm).
Combining Node.js packages with TypeScript can help simplify code management and increase productivity. In this article, we will discuss the process of creating a Node.js package with TypeScript and explore the advantages of using this approach.
Building packages in Node.js is important for several reasons:
By creating packages, developers can write code once and reuse it across multiple projects. This saves time and effort in the long run and reduces the amount of duplicated code.
Packages are modular units of code that can be easily updated, modified, or replaced without affecting other parts of the application. This makes it easier to maintain and scale applications over time.
Node.js packages come with a package.json file that lists all the dependencies required for the package to function properly. This makes it easier to manage dependencies and ensure that the package runs consistently across different environments.
Packages can be published to npm, a package repository that allows developers to share their packages with the wider community. This fosters collaboration and allows developers to receive feedback and improve their packages.
By using packages, developers can focus on writing code that is specific to their project instead of writing code that has already been written before. This can help speed up development and increase productivity.
Overall, building packages in Node.js helps simplify code management, increase productivity, and encourages collaboration within the community. It also helps ensure code consistency and maintainability over time.
Good to know before starting to create your Node.js:
- Basic understanding in Typescript
- Basic understanding on how npmjs.com works
- Basic understanding of Node.js
What you will learn by reading this:
- Export a typed function
- Publish a Node.js library to npm
Why would you want to learn this?
- Typescript usage has grown a lot over the last few years.
- You want to share a library to friends, colleagues for everyone to use.
- You want feedback on a library/project you have been working on a while.
We will create a module with a function whose sole purpose is to sum two numbers, just to keep it simple.
We’ll start by creating a new folder in our operating system of preference, since I’m on a mac, I’ll open the terminal and create a folder in my home folder called suma-dos (this name has to be unique, since it will be the package name in npmjs.com), I do this with the mkdir command:
then I will cd into the folder with the cd command:
At this point, we have to initialize our npm project, we do that by writing this command in our terminal:
this only creates a package.json file with some default values:
Now, we will start writing some code, we create a folder, let’s call it src, and add a file there named:
so far our project looks like this:
Let’s update our src/index.ts file, and we are going to add a function, whose job is to sum two numbers, we write it like this:
We can see our function is fully typed, we have two arguments which are numbers, and the return value is also a number, and then we use a named export.
That will install the binary we need to transpile the code, now, we will create a script in our package.json file for that, so we now have in our package.json this content:
Finally, to transpile our typescript file, we need a base config for Typescript binary to work on, and we do that by writing this command in our terminal:
This will create this file (tsconfig.json):
This might seem overwhelming at first… because it is, but we only need a couple of them, so we will replace that with our own:
If you want to know more about this file, you can always check the tsconfig.json docs.
Let’s now transpile our code, by running the typescript script, we run in our console the next command:
Publishing our package to npmjs.com
First off, we need to update our package.json file, since npm use some fields from this file at the time of publishing:
- name: this has to be an unique name, make sure it doesn’t exists by searching in npmsjs.com first.
- version: this has to be unique in each deploy, since you can’t publish a package with the same version twice.
- types: here we define where our types are, so another tools using our package (npmjs.com, vscode, and others), know where to find them.
Our package.json should look like this at this point:
If we want to see what files we are going to publish, we can execute this command in our terminal:
And we should get something like this:
We are almost there, we need a npmjs.com account, if you don’t have one, you can create it here.
If you do already have one, we have to authenticate the npm cli tool with npmjs.com:
We go through each of the steps, where we input our username, password, and email, and OTP if enabled on the account. We can then validate that we did everything right by executing a command:
And this should return our username, finally we can enter our last command to publish our Node.js library to npm:
You should get something like this in your console:
And something like this in https://www.npmjs.com/package/suma-dos:
Now we can immediately share our library with our colleagues and/or friends, we let them know the name of our library (suma-dos) and then they can install with:
And finally to use our library in a Node.js project we will do it like this:
What did you learn?
If you managed to get this far, this a summary of what you learned:
- Make a small typescript library in Node.js
- You know how to type your exported functions
- You know how to publish your library to npmjs.com
What are Node.js packages and how can they be used?
Node.js packages are modules of reusable code that can be easily shared and integrated into other projects. Developers can create packages to write code once and reuse it across multiple projects, saving time and effort. These packages can be published and downloaded from the Node Package Manager (npm), allowing for easy sharing and collaboration within the developer community.
What is TypeScript and why is it useful in combination with Node.js packages?
Why is building packages in Node.js important?
Building packages in Node.js offers several benefits. They allow developers to write code once and reuse it across multiple projects, reducing duplication and saving time and effort. Packages are modular units of code that can be easily updated, modified, or replaced without affecting other parts of the application, making it easier to maintain and scale applications over time. Node.js packages also simplify dependency management and encourage collaboration within the developer community, fostering feedback and improvement. Finally, leveraging packages can increase productivity by allowing developers to focus on project-specific code rather than reinventing existing solutions.
What are the steps involved in creating a Node.js package with TypeScript?
How can I publish my Node.js package to npmjs.com?