No description
Find a file
2025-02-17 15:20:07 +08:00
utils Add sample modules 2025-02-16 15:52:29 +08:00
.gitignore Ignore npmrc 2025-02-16 21:54:53 +08:00
index.js Rename func 2025-02-16 15:53:31 +08:00
package.json Update author with email and link 2025-02-17 15:19:56 +08:00
README.md Add link to package.json ref 2025-02-17 15:20:07 +08:00

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:

  1. Set a scoped name to the package (i.e. @irfanhakim/npm-pkg-demo).

  2. (Optional) Specify the files to be included in the package:

    "files": [
        "index.js",
        "utils/"
    ],
    
  3. Add a publishConfig object to specify the registry:

    "publishConfig": {
        "@irfanhakim:registry": "https://gitlab.com/api/v4/projects/67162761/packages/npm/"
    },
    

    Replace irfanhakim with the scoped name (i.e. Gitlab username) and 67162761 with the repository's project ID.

  4. (Optional) Specify the author or maintainer of the package:

    "author": "Irfan Hakim <irfanhakim.as@yahoo.com> (https://links.irfanhak.im)",
    
  5. Authenticate to the package registry for the project:

    npm config set -- //gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken=${GITLAB_TOKEN}
    
    • Replace 67162761 with 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_registry
        • write_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_TOKEN variable.

    • (Optional) To not have to authenticate manually, create an .npmrc file in the root of the repository:

      //gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken="${GITLAB_TOKEN}"
      
  6. Publish the package:

    npm publish
    

How To Install

On the separate project where you wish to install the package:

  1. Authenticate to the target package registry:

    npm config set -- //gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken=${GITLAB_TOKEN}
    
    • Replace 67162761 with the target package registry's project ID.

    • Acquire and set the target package registry's access token as the GITLAB_TOKEN variable in your shell environment.

    • (Optional) To not have to authenticate manually, create an .npmrc file in the root of your project:

      //gitlab.com/api/v4/projects/67162761/packages/npm/:_authToken="${GITLAB_TOKEN}"
      
  2. Configure the target package registry URL:

    npm config set @irfanhakim:registry=https://gitlab.com/api/v4/projects/67162761/packages/npm/
    
    • Replace irfanhakim with the actual scope to the target package registry.

    • Replace 67162761 with the target package registry's project ID.

    • (Optional) To not have to configure this manually, add the following line to an .npmrc file in the root of your project:

      @irfanhakim:registry=https://gitlab.com/api/v4/projects/67162761/packages/npm/
      
  3. Install the package:

    npm install @irfanhakim/npm-pkg-demo
    
    • Replace irfanhakim with the actual scope to the target package registry.

    • Replace npm-pkg-demo with the name of the target package.

    • Alternatively, to install the package as a dev dependency, add the --save-dev flag to the install command:

      npm install --save-dev @irfanhakim/npm-pkg-demo
      
  4. After the package is installed, your project's package.json file should have been updated like so:

    "dependencies": {
        "@irfanhakim/npm-pkg-demo": "^1.0.0"
    }
    

    If it was installed as a dev dependency, the package.json file would have been updated like the following instead:

    "devDependencies": {
        "@irfanhakim/npm-pkg-demo": "^1.0.0"
    }
    
    
  5. 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);
    }