Development Tutorials 11 min read

# How to create a private npm package on GitLab

Find out how to create your own private npm packages on GitLab with the help of our guide.

Last updated on March 23, 2026

## Key takeaways

1. Turning frequently reused code into a private npm package on GitLab makes it easier to install, maintain and update across projects.
2. Setting libraryTarget to umd in the build configuration lets the package work with CommonJS, AMD and as a global variable.
3. The package name should follow the @gitlab-username/repository-name pattern, with publishConfig pointing to the GitLab project's npm registry URL.
4. A deploy token with read and write package registry rights authenticates access, and the.npmrc file containing it should be added to.gitignore.
5. A.gitlab-ci.yml deploy job publishes the package, and the version in package.json must be increased with each commit to the main branch or the job fails.

This guide will teach you how to **create your own private npm packages** on GitLab. Having code that you often reuse from project to project is difficult to maintain, and it is annoying having to copy-paste it all the time. Making those chunks of code into npm packages can help you to easily install them on any project, maintain and update their codebase and apply updates on projects where they are used. Keep reading and find out what to do.

## Project setup

First things first, you need to **create a new repository on GitLab**, which will be dedicated for your npm package. Once you have created a repository, clone it.

After that, create a **folder structure**. For the purposes of this guide, we’ll have an src folder with an index.js file containing a simple Rectangle class with a constructor that accepts width and height, and a method that calculates the area. At the end class is exported as a module.

![project setup](https://factory.dev/wp-content/uploads/2026/11/1-project-setup.webp)

Since we are using ES6 JavaScript, we need to **transpile** the code. For that, we are using Laravel Mix, but you can use just Webpack (or any other tool you prefer). The important thing is that you **configure the output** by setting libraryTarget to umd and library to NameOfYourLibrary. That exposes your library under all the module definitions, allowing it to work with CommonJS, AMD, and as a global variable.

![project setup example](https://factory.dev/wp-content/uploads/2026/11/2-project-setup.webp)

### Package.json

If you already have the package.json file, you will need to adjust a few things, but in case you don’t, you can **run npm init** to set it up or create it manually. The first thing you should check is the name; it should be **@your-gitlab-username/your-repository-name.**

After that, make sure that you have a version; starting version should be 1.0.0. Next are main and files; main is the path to the main file that your library will be imported from and files is the array of folders or files that should be included in the package. In our case, we have main set to **dist/index.js**, which is a transpiled and minified version of the index.js file from the src folder, and files containing dist and src folders.

Last but not least is publishConfig which should be:

*“@your-gitlab-username:registry”: “https://gitlab.com/api/v4/projects/your-project-id/packages/npm/”.*

If you have GitLab on your custom domain, then replace gitlab.com with your own domain. As for the project id, it can be found on the repository page right under the repository name.

![package json](https://factory.dev/wp-content/uploads/2026/11/3-package-json.webp)

![project id](https://factory.dev/wp-content/uploads/2026/11/4-project-id.webp)

## .npmrc file and token

Create a file named.npmrc and add the following lines to it:

*@your-gitlab-username:registry https://gitlab.com/api/v4/packages/npm/*

*//gitlab.com/api/v4/packages/npm/:\_authToken=\<token>*

![npmrc](https://factory.dev/wp-content/uploads/2026/11/5-npmrc.webp)

Before we go into creating and adding a token, you should add the.npmrc file to **.gitignore** because files containing tokens shouldn’t be committed to a repository.

To **create a token**, you need to go to the repository page and then Settings -> Repository and expand the section Deploy tokens. There you can create a new token by adding a name and make sure to check **read\_package\_registry** and **write\_package\_registry**.

![create token](https://factory.dev/wp-content/uploads/2026/11/6-create-token.webp)

Once you have created a token, you will see the password for it. Copy the password, go to Settings -> CI/CD, and expand the section Variables. There you can add a new variable, add some name under the key for value, paste the password and check Mask variable checkbox.

![save token](https://factory.dev/wp-content/uploads/2026/11/7-save-token.webp)

In the.npmrc file, replace the \<token> with the actual token/password.

## Publishing npm package

To publish an npm package, you need to have a deploy job and a runner that will run it. If you already have a runner, you can use that one; for that, check Settings -> CI/CD -> Runners, or if you don’t have it there, you’ll find a guide on how to set it up. For a deploy job, create a.gitlab-ci.yml file with the following code:

*image: node:latest*

*stages:*

*– deploy*

*deploy:*

*stage: deploy*

*script:*

*– echo “//${CI\_SERVER\_HOST}/api/v4/projects/${CI\_PROJECT\_ID}/packages/npm/:\_authToken=${CI\_JOB\_TOKEN}”>.npmrc*

*– npm publish*

*environment: production*

Now you can commit and push changes. Then go to your repository and check the deploy job status (CI/CD -> Jobs). It will tell you if the job was successful or failed and what went wrong. One important thing to note is every commit you make to the main/master branch, you have to also update the version in the package.json file; otherwise, the job will fail.

![job example](https://factory.dev/wp-content/uploads/2026/11/8-job.webp)

Once the job is done, and it was successful, your npm package is published. You can check it by going to Packages &amp; Registries.

When that package starts being used across multiple projects, it becomes part of a broader system landscape rather than an isolated piece of code. In larger environments, managing private npm packages is only one part of the overall system. As the number of services, dependencies, and deployments grows, maintaining consistency across environments becomes more complex. This is typically addressed through ongoing [system maintenance](https://factory.dev/services/pimcore-maintenance).

![package registry](https://factory.dev/wp-content/uploads/2026/11/9-package-registry.webp)

## Using the npm package on other projects

You can install this npm package on any of your new or existing projects. What you need to do is create a.npmrc file inside of that project by running the following command:

*echo @your-gitlab-username:registry=https://gitlab.com/api/v4/packages/npm/ >>.npmrc*

Then open the.npmrc file and add the following **//gitlab.com/api/v4/packages/npm/:\_authToken=\<token>** at end, replace \<token> with the actual token. You can easily find the token anytime by going to your npm package repository and then Settings -> CI/CD -> Variables and copy it from there.

![project npmrc](https://factory.dev/wp-content/uploads/2026/11/10-project-npmrc.webp)

Once you have done this, you can install the npm package by running the following command:

*npm i @your-gitlab-username/your-npm-package-repository-name*

![install package](https://factory.dev/wp-content/uploads/2026/11/11-install-package.webp)

And then import it in your project JavaScript file:

*import YourLibrary from
‘@your-gitlab-username/your-npm-package-repository-name’;*

Now you have everything you need to create your own private npm packages on GitLab! I hope this guide will be helpful to you, and if you have any questions about this topic, we’ll be happy to answer them, so feel free to [reach out](https://factory.dev/contact).

## Related

[Development Tutorials **How to Make Custom InDesign Plugins with UXP and React** Learn how to create InDesign plugins using UXP and React to display and fill in product data fetched from the web directly in your InDesign documents. 17 min read](https://factory.dev/blog/custom-indesign-plugins-uxp-react) [Development Tutorials **Generate Critical CSS (Above the fold) for Specific PimCore Documents** Critical CSS can be a beneficial technique to optimize and minimize your initial page loading time. Discover how to implement it for Pimcore Documents. 12 min read](https://factory.dev/blog/critical-css-above-fold-pimcore-documents) [Development Tutorials **Integrating a New Payment Provider into Coreshop** Using this guide, you will be able to implement a new payment provider using Payum and Coreshop. Keep reading and find out what to do. 6 min read](https://factory.dev/blog/integrating-new-payment-provider-coreshop)

---

Source: https://factory.dev/blog/private-npm-package-gitlab
