Introducci贸n a Node.js
Introducci贸n Node.js
Instalaci贸n y configuraci贸n del entorno de Node.js
Primer proyecto con Node.js
Quiz: Introducci贸n a Node.js
M贸dulos y gesti贸n de paquetes
Tipos de M贸dulos en Node.js
Gesti贸n de Paquetes con NPM
Creaci贸n de un Paquetes con NPM
Publicaci贸n de Paquetes con NPM
Quiz: M贸dulos y gesti贸n de paquetes
M贸dulos nativos en Node.js
Introducci贸n al M贸dulo FS de Node.js
Leer y escribir archivos en Node.js
M贸dulo fs: Implementar transcripci贸n de audio con OpenAI
M贸dulo Console: info, warn, error, table
M贸dulo Console: group, assert, clear, trace
M贸dulo OS: informaci贸n del sistema operativo en Node.js
M贸dulo Crypto: cifrado y seguridad en Node.js
M贸dulo Process: manejo de procesos en Node.js
Timers: setTimeout, setInterval en Node.js
Streams: manejo de datos en tiempo real en Node.js
Buffers: manipulaci贸n de datos binarios en Node.js
Quiz: M贸dulos nativos en Node.js
Servidores con Node.js
HTTP: fundamentos de servidores en Node.js
Servidor nativo y streaming de video en Node.js
You don't have access to this class
Keep learning! Join and start boosting your career
Building packages in NPM is a fundamental skill for any Node.js developer who wants to share their solutions with the community. Learning how to build, test and publish a package not only enhances your professional profile, but also contributes to the JavaScript ecosystem, allowing other developers to leverage your code to solve common problems.
Creating an NPM package requires following a series of structured steps to ensure that our code is accessible and usable by other developers. In this case, we are going to build a package called "platzi-date" that will allow us to handle dates in two specific formats: timestamp and long format.
To start creating our package, we need to follow these initial steps:
mkdir platzi-datecd platzi-date
git init
npm init -y
This last command will automatically generate a package.json
file with the basic configuration that we will later modify according to our needs.
Once the project is initialized, it is time to create the file structure and develop the package functionality:
src
folder for the source code.index.js
file inside the src
folder .
In our index.js
file, we will implement the two main functions:
// Function to get the current timestampfunction getTimeStamp() { return Date.now();}
// Function to get the date in long formatfunction getLongTime(locale = 'en') { const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', timeZoneName: 'short' };
return new Date().toLocaleDateString(locale, options);}
// Export functionsmodule.exports = { getTimeStamp, getLongTime};
The getTimeStamp()
function simply returns the current time value in milliseconds since January 1, 1970, using the native Date.now()
method.
The getLongTime()
function is more complex and allows us to format the current date in a long format, specifying the language using the locale
parameter (which defaults to English). This function uses the toLocaleDateString()
method of JavaScript, which accepts formatting options to customize the output.
A good package should always include clear documentation. We will create a README.md file:
# platzi-date
A utility to handle dates in timestamp and long time format.
##Install
````bashnpm install platzi-date.
This basic documentation explains the purpose of the package and how to install it.In a real package, it would be advisable to add usage examples, detailed API and any other relevant information.
### Configuring package.json
The ``package.json`` file is crucial for the release of our package.We must modify it to include important information:
````json{ "name": "platzi-date", "version": "1.0.0", "description": "A utility to handle dates in timestamp and long time format", "main": "src/index.js", "keywords": ["date", "time", "timestamp"], "author": "Your Name", "license": "MIT"}
The most important fields are:
Before publishing our package, it is essential to test it locally to make sure it works correctly. NPM provides a tool called npm link
that allows us to create a symbolic link to our local package.
To create the link, follow these steps:
npm link
npm link platzi-date
This creates a symbolic link in the node_modules
folder of our project, pointing to the local package we are developing.
To test our package, we create a test file in our main project:
// date.jsconst dateFormatter = require('platzi-date');
// Get current timestampconsole.log(dateFormatter.getTimeStamp());
// Get date in Spanish (default format)console.log(dateFormatter.getLongTime());
// Get date in Englishconsole.log(dateFormatter.getLongTime('en-US'));
When running this file with Node.js:
node date.js
We should see three outputs:
If everything works correctly, it means that our package is ready to be published.
Before publishing our package in NPM, it is important to make some final checks:
It is essential to perform these checks because once the package is published, although new versions can be published, the original version will be registered in NPM and could be downloaded by other developers.
Creating and publishing packages in NPM is a great way to contribute to the development community and share useful solutions. With this process, you have learned how to create a simple but functional package for date management. What other types of utilities do you think you could develop and share with the community? Share your ideas in the comments!
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?