| utils | ||
| .gitignore | ||
| index.js | ||
| package.json | ||
| README.md | ||
npm-pkg-demo
This repository is a simple demonstration on how to publish an npm package on GitLab (gitlab.com).
References
Public Registry
What if the package you wish to publish or install is hosted on a public GitLab repository?
- Publish: Steps to publish your package to the public registry remains exactly the same.
- Install: Steps to install your package from the public registry remains largely the same, but you can omit the step to authenticate to the package registry.
How To Publish
Update the package's package.json file with the following configuration:
-
Set a scoped name to the package (i.e.
@irfanhakim/npm-pkg-demo). -
(Optional) Specify the files to be included in the package:
"files": [ "index.js", "utils/" ], -
Add a
publishConfigobject to specify the registry:"publishConfig": { "@irfanhakim:registry": "https://gitlab.com/api/v4/projects/67162761/packages/npm/" },Replace
irfanhakimwith the scoped name (i.e. Gitlab username) and67162761with the repository's project ID. -
(Optional) Specify the author or maintainer of the package:
"author": "Irfan Hakim <irfanhakim.as@yahoo.com> (https://links.irfanhak.im)", -
Authenticate to the package registry for the project:
npm config set -- //gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken=${GITLAB_TOKEN}-
Replace
67162761with the repository's project ID. -
Generate a GitLab access token if you have not already:
-
If you are using a project-level deploy token, the following scope(s) are required:
read_package_registrywrite_package_registry
-
If you are using a user-level access token, the following scope(s) are required:
api
-
Once the token is generated, add it to your shell environment as the
GITLAB_TOKENvariable.
-
-
(Optional) To not have to authenticate manually, create an
.npmrcfile in the root of the repository://gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken="${GITLAB_TOKEN}"
-
-
Publish the package:
npm publish
How To Install
On the separate project where you wish to install the package:
-
Authenticate to the target package registry:
npm config set -- //gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken=${GITLAB_TOKEN}-
Replace
67162761with the target package registry's project ID. -
Acquire and set the target package registry's access token as the
GITLAB_TOKENvariable in your shell environment. -
(Optional) To not have to authenticate manually, create an
.npmrcfile in the root of your project://gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken="${GITLAB_TOKEN}"
-
-
Configure the target package registry URL:
npm config set @irfanhakim:registry=https://gitlab.com/api/v4/projects/67162761/packages/npm/-
Replace
irfanhakimwith the actual scope to the target package registry. -
Replace
67162761with the target package registry's project ID. -
(Optional) To not have to configure this manually, add the following line to an
.npmrcfile in the root of your project:@irfanhakim:registry=https://gitlab.com/api/v4/projects/67162761/packages/npm/
-
-
Install the package:
npm install @irfanhakim/npm-pkg-demo-
Replace
irfanhakimwith the actual scope to the target package registry. -
Replace
npm-pkg-demowith the name of the target package. -
Alternatively, to install the package as a dev dependency, add the
--save-devflag to the install command:npm install --save-dev @irfanhakim/npm-pkg-demo
-
-
After the package is installed, your project's
package.jsonfile should have been updated like so:"dependencies": { "@irfanhakim/npm-pkg-demo": "^1.0.0" }If it was installed as a dev dependency, the
package.jsonfile would have been updated like the following instead:"devDependencies": { "@irfanhakim/npm-pkg-demo": "^1.0.0" } -
You can now use the installed package in your project:
const npmPkgDemo = require("@irfanhakim/npm-pkg-demo"); if (require.main === module) { const results = npmPkgDemo.test(1, 2); console.log(results); }