iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📦

Distributing and Using Internal npm Packages with GitHub Packages

に公開

This content was something I quickly wrote up thinking I would post it on the company blog, but I decided against it since it didn't have much novelty. It felt like a waste to just throw it away, so I'm posting it on Zenn instead.


I will introduce the workflow for distributing an internal npm package using GitHub Packages and using it in an actual project.

Publishing the Package

First, I'll create a repository called https://github.com/st-tech/sample-private-npm-package. This is where I'll place the package source code.

I'll create a new project directory and initialize a package.json.

{
  "name": "@st-tech/sample-private-npm-package",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Kaido Iwamoto <kaido.iwamoto@zozo.com>",
  "license": "ISC"
}

It remains mostly the boilerplate generated by npm init, but the key point is that the package name includes the @st-tech scope.
In this case, the scope must match the GitHub organization name.

I'll also create a .gitignore file.

node_modules
.vscode
dist

Since I'm at it, let's write it in TypeScript and add Prettier. Using TypeScript is mainly to add a bit more substance to the article.

$ npm install -D typescript prettier

I'll write a minimal tsconfig.json.

{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "declaration": true,
    "strict": true,
    "outDir": "./dist"
  },
  "include": ["./src/**/*"],
  "exclude": ["node_modules"]
}

Now, I'll create src/index.ts. There's no point in writing anything complex, so I'll just add one simple function.

export function sayHello(name: string): void {
  console.log("Hello", name);
}

I'll log in to the GitHub Package Registry using the npm CLI. Use your GitHub account username and password. However, if you have two-factor authentication enabled on GitHub, you must generate and use a Personal Access Token.

For the Personal Access Token permissions, check repo, read:packages, write:packages, and delete:packages. Also, if your GitHub organization uses SSO, ensure you authorize the SSO for the token.

$ npm login --registry=https://npm.pkg.github.com

I'll modify package.json slightly, adding the scripts and publishConfig.

The final package.json looks like this:

{
  "name": "@st-tech/sample-private-npm-package",
  "version": "0.0.1",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "tsc"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/st-tech"
  },
  "author": "Kaido Iwamoto <kaido.iwamoto@zozo.com>",
  "license": "ISC",
  "devDependencies": {
    "prettier": "^2.1.1",
    "typescript": "^4.0.2"
  }
}

Now that everything is ready, let's publish the package.

$ npm publish
 
> @st-tech/sample-private-npm-package@0.0.1 prepare .
> tsc
 
npm notice
npm notice 📦  @st-tech/sample-private-npm-package@0.0.1
npm notice === Tarball Contents ===
npm notice 193B dist/index.js 
npm notice 456B package.json  
npm notice 206B tsconfig.json 
npm notice 54B  dist/index.d.ts
npm notice 79B  src/index.ts  
npm notice === Tarball Details ===
npm notice name:          @st-tech/sample-private-npm-package    
npm notice version:       0.0.1                                  
npm notice package size:  735 B                                  
npm notice unpacked size: 988 B                                  
npm notice shasum:        d45c9f685399a4419fe10f5f4828dc95be2649c3
npm notice integrity:     sha512-w75wwY5EetBc0[...]kY/7DG3//+VoQ==
npm notice total files:   5                                      
npm notice
+ @st-tech/sample-private-npm-package@0.0.1

By the way, I usually use yarn instead of npm, but I use npm when publishing packages to GitHub Packages.

Checking the package page on GitHub, it has been published successfully.

Next, let's use the published package from another project.

Using the Package

I'll create an arbitrary npm project. I'll create a package.json and install a few development packages. I'll use TypeScript here as well. The tsconfig.json can be the same as the one used earlier.

$ npm install -D prettier typescript ts-node

Next, I'll create an .npmrc file in the project directory and add the following. By adding this setting, scoped packages prefixed with @st-tech/ will be installed from GitHub Packages.

@st-tech:registry=https://npm.pkg.github.com

Now, I'll install the package created earlier.

$ npm install @st-tech/sample-private-npm-package

The final package.json looks like this:

{
  "name": "iwamoto-sample-npm-project",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "prettier": "^2.1.2",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.2"
  },
  "dependencies": {
    "@st-tech/sample-private-npm-package": "0.0.1"
  }
}

Finally, I'll create src/index.ts and write the following code.

import { sayHello } from "@st-tech/sample-private-npm-package";
 
const name = "Kaido Iwamoto";
sayHello(name);

Running this, I was able to successfully call the code from the package I created.

$ ./node_modules/.bin/ts-node src/index.ts
 
Hello Kaido Iwamoto

Discussion