Learn to test your packages NPM locally.
Ivan Zaldivar
Posted on November 11, 2021
Hi guys! In this post we are going to learn how to test the NPM package before being deployed to NPM. and that other developers can use it.
Initialize project.
In this part, we are going to initialize a simple Node.js project that allows us to perform basic mathematical operations. Get started!
Create directory.
mkdir operations-basic
Enter directory.
cd operations-basic
Initialize project NPM
npm init -y
Initialize project with Typescript
tsc --init
Perfect, now we create src/index.ts and add the following content.
> src > index.ts
export function sum(a: number, b: number): number {
return a + b;
}
export function subtract(a: number, b: number): number {
return a - b;
}
export function multiplication(a: number, b: number): number {
return a * b;
}
export function division(a: number, b: number): number {
if (b < 0) throw new Error("Cannot divide by zero");
return a / b;
}
Nice, now we update tsconfig.json and add the following content.
> tsconfig.json
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Language and Environment */
"target": "ES5",
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"baseUrl": "./src",
/* Emit */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
"outDir": "./lib", /* Specify an output folder for all emitted files. */
/* Interop Constraints */
"esModuleInterop": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
"strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
"strictFunctionTypes": true, /* Ensure 'use strict' is always emitted. */
"noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
"noUnusedParameters": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
Magnificent, we update package.json file.
> package.json
{
"name": "operations-basic",
"version": "0.1.0",
"description": "Little descriptions of what your package does",
"main": "lib/index.js",
"types": "lib",
"scripts": {
"build": "tsc -p ."
},
"keywords": ["Some keywords"],
"author": {
"name": "<Your name>",
"email": "<Your email>",
"url": "https://github.com/<Your username>"
},
"repository": {
"url": "https://github.com/<Your username>/<Package name>"
},
"license": "MIT",
"dependencies": {
},
"devDependencies": {
}
}
Testing locally.
Now, we execute npm run build
this command generate a directory lib which will be the one that will be uploaded to NPM, with its declarations, mappings, etc.
Beautiful. in this you should be able to see the lib folder, if so, run the following command.
npm link
What this command will do is a symbolic link, with the name of your folder, and it will be added to the global packages on your computer. Similar when you download a package as follows: npm installl -g some-package
Note: Remember that it is a symbolic link, so any change you make, you will only have to compile it and then execute
npm link
and the changes will be reflected in the repositories where you use it.
Perfect, now it is created, now we can add to this package with the project that we want to test. And we do it in the following way.
npm link package-name
Note: In the case that you want to test multiple packages at the same time, it is necessary that you execute them in a single command.
npm link package-name-one package-name-two package-name-three
Considerations.
- It is very important that the package name is unique.
Well guys, this is it for today. You already know that if you have a more efficient way to test an npm package locally, feel free to share it in the comments, it would be of great help.
Follow me on social networks.
- 🎉 Twitter: https://twitter.com/ToSatn2
- 💡 Github: https://github.com/IvanZM123
Posted on November 11, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.