Publish private NPM package to Gitlab
Table of Contents
Introduction
This tutorial will guide you through the process of publishing a private NPM package to GitLab. By the end of this guide, you'll understand how to set up your Node package, update it for GitLab, configure your GitLab token, and utilize the package from the GitLab registry. This is particularly useful for developers looking to manage their packages securely within their GitLab projects.
Step 1: Set Up and Test Node Package Locally
-
Create a new directory for your package:
- Open your terminal and run:
mkdir my-package cd my-package
- Open your terminal and run:
-
Initialize the Node package:
- Use the following command to create a
package.json
file:npm init -y
- Use the following command to create a
-
Create an index file:
- Create a file named
index.js
in your package directory:touch index.js
- Add some sample code to
index.js
. For example:module.exports = () => { console.log("Hello from my package!"); };
- Create a file named
-
Test your package locally:
- Within the package directory, run:
node index.js
- Ensure it outputs "Hello from my package!".
- Within the package directory, run:
Step 2: Update Package for GitLab
-
Modify package.json for GitLab:
- Open
package.json
and add the following fields:{ "name": "@your-username/my-package", "version": "1.0.0", "publishConfig": { "registry": "https://gitlab.com/api/v4/packages/npm/" } }
- Replace
your-username
with your GitLab username.
- Open
-
Add a .gitignore file (optional):
- Create a
.gitignore
file to exclude unnecessary files:touch .gitignore
- Add
node_modules
anddist
to the.gitignore
file.
- Create a
Step 3: Set Up GitLab Token
-
Create a personal access token in GitLab:
- Go to GitLab and navigate to User Settings > Access Tokens.
- Name your token, select the
read_api
andwrite_api
scopes, and create the token.
-
Set the GitLab token in your environment:
- In your terminal, run:
export NPM_TOKEN=your_personal_access_token
- Replace
your_personal_access_token
with the token you just created.
- In your terminal, run:
Step 4: Publish Package to GitLab
-
Login to GitLab NPM registry:
- Use the following command to authenticate:
npm login --scope=@your-username --registry=https://gitlab.com/api/v4/packages/npm/
- Enter your GitLab username and the token as the password.
- Use the following command to authenticate:
-
Publish your package:
- Run the command:
npm publish
- Run the command:
Step 5: Using Package from GitLab Registry
-
Install your package in another project:
- In your project directory, run:
npm install @your-username/my-package --registry=https://gitlab.com/api/v4/packages/npm/
- In your project directory, run:
-
Import and use your package:
- In your project, you can now use it like this:
const myPackage = require('@your-username/my-package'); myPackage(); // Outputs: Hello from my package!
- In your project, you can now use it like this:
Conclusion
You have successfully published a private NPM package to GitLab and learned how to use it in your projects. Key takeaways include setting up your package correctly, configuring GitLab tokens, and managing your packages securely. For further exploration, consider looking into more advanced CI/CD integrations with GitLab for automated deployments and package management.