在本文中,我想展示我们如何轻松创建我们的第一个 。 私有 npm 包 我们将在 创建和发布一个私有包,因此在开始本文之前,请确保您已熟悉 。所以让我们开始吧。 GitHub 上 先决条件 先决条件: 创建一个 帐户( ) GitHub 如果您还没有 阅读文章 如何使用 TypeScript 设置 Node Express 阅读文章 ( ) 如何使用 GitHub Actions 从头开始设置 CI CD 管道 如果您不熟悉 GitHub Actions,我强烈建议您阅读本文以了解 GitHub Actions 的工作原理 什么是私有 npm 包以及它可以在哪里使用? 使用私有 npm 包,您可以托管仅对您和有权访问的人可见的代码,从而允许您在项目中管理和使用私有代码以及公共代码。 项目结构 math-lib/ --.github/ --workflows/ --main.yml --build/ this is autogenerated folder --src/ --app.ts --types/ --index.d.ts --.gitignore --package.json --tsconfig.json 构建私有 npm 包 第 1 部分: 使用 TypeScript 初始化一个 Node.js 项目。 第 1 步: 来学习如何做到这一点, 注意: 您可以通过阅读这篇文章 How to Setup a Node Express with TypeScript 或者您可以克隆这个 repo https://github.com/YuraAbharian/node-express-typescript 现在让我们创建 GitHub Actions 工作流程。 第 2 步: 创建一个 目录 .github/workflows 在 目录下,创建一个 文件 .github/workflows main.yml 您可以通过阅读这篇文章 熟悉 GitHub Actions 工作流程 How to Setup a CI CD Pipeline From Scratch with GitHub 来 让我们添加一些配置: name: Node.js Private Package on: push: branches: - master jobs: publish-gpr: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '14.x' registry-url: 'https://npm.pkg.github.com/' - run: npm install - run: npm run build - run: npm publish env: NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} : 注意 记住 我们稍后会详细讨论 GITHUB_TOKEN, 让我们创建/更新 文件 第 3 步: src/app.ts export function sum(a: number, b: number): number { return a + b; } export function minus(a: number, b: number): number { return a - b; } export function divide(a: number, b: number): number { return a / b; } export function multiple(a: number, b: number): number { return a * b; } 现在我们需要为我们的包声明一个模块 第 4 步: 在项目的根目录下创建一个 目录 types 在 目录中创建一个 文件 types index.d.ts 为这个模块添加声明类型 declare module "@GITHUB_USERNAME/PACKAGE_NAME" { function sum(a: number, b: number): number; function minus(a: number, b: number): number; function divide(a: number, b: number): number; function multiple(a: number, b: number): number; } 笔记: - 这是你的 github 用户名 GITHUB_USERNAME - 这是你的私人包名 PACKAGE_NAME 示例: “@yuraabharian/math-lib” 让我们处理 package.json 文件 第 5 步: { "name": "@GITHUB_USERNAME/PACKAGE_NAME", "version": "1.0.0", "description": "", "main": "./build/app.js", "author": "", "license": "ISC", "types": "./types/index.d.ts", "publishConfig": { "registry": "https://npm.pkg.github.com/" }, "repository": { "url": "git://github.com/GITHUB_USERNAME/PACKAGE_NAME.git" }, "scripts": { "build": "npx tsc" }, "devDependencies": { "@types/node": "^18.0.0", "typescript": "^4.7.4" } } 示例: git://github.com/yuraabharian/math-lib.git(这将是您 GitHub 存储库的链接) 注意: 这些@GITHUB_USERNAME/PACKAGE_NAME 字段与 中的相同 步骤 4 让我们在推送代码之前配置我们的存储库环境 第 6 步: 在 中,我要求您记住 ,因为这个变量非常重要。 第 2 步 GITHUB_TOKEN 转到 GitHub Repository - Settings 然后去 第7步: Secrets 打开 第 8 步: Actions 单击 New repository secret 通过此链接 生成新令牌。 必需: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token 。 注意: 将 保存在别处,因为我们将在本文的第 2 部分中需要它们 GITHUB_TOKEN 此令牌的 注意: 授予 仅 write:packages 访问权限 现在让我们将代码推送到 第 9 步: GitHub 打开 ,您应该会看到您的包已部署 第 10 步: 存储库 → 操作 要查找您的包,请转到您的 第 11 步: GitHub 个人资料 → 包 第一部分的结论:此时您应该看到您的包已部署 在本文的第二部分,我们将学习如何安装私有包,因为它需要一些操作,除了 。 npm install package 安装和测试 第 2 部分: 设置 Node.js 项目 第 1 步: 您可以在 中查看如何执行此操作 注意: 第 1 部分 → 步骤 1 然后在项目的根目录中插入您的包。你可以通过 获取你的包的链接(这是你的包名) 第 2 步: GitHub profile → Packages → math-lib 现在转到 src/app.ts 文件并更新它 第 3 步: import express, {Application, Request, Response} from 'express'; import {sum, minus, multiple, divide} from '@yuraabharian/math-lib'; const app: Application = express(); const PORT: number = 3001; app.use('/sum', (req: Request, res: Response): void => { res.send(`RESULT: ${sum(5, 2)}`, ); }); app.use('/minus', (req: Request, res: Response): void => { res.send(`RESULT: ${minus(2, 2)}`); }); app.use('/multiple', (req: Request, res: Response): void => { res.send(`RESULT: ${multiple(12, 2)}`); }); app.use('/divide', (req: Request, res: Response): void => { res.send(`RESULT: ${divide(10, 2)}`); }); app.listen(PORT, (): void => { console.log('SERVER IS UP ON PORT:', PORT); }); 请记住,我正在从我的 @yuraabharian/math-lib 存储库中导入 math-lib 以使您的模板看起来像 @GITHUB_USERNAME/PACKAGE_NAME 注意: 运行您的项目 第 4 步: npm start node-express-typescript % npm start > express-typescript@1.0.0 start > npx tsc && node build/app.js SERVER IS UP ON PORT: 3001 :转到您的浏览器并打开 第 5 步 http://localhost:3001/sum 这行得通,所以现在让我们测试所有方法: http://localhost:3001/多个 http://localhost:3001/减号 http://localhost:3001/divide 在本文中,我们创建并测试了您的第一个私有 npm 包,希望您喜欢我的文章。如果您有任何问题,可以通过电子邮件、LinkedIn 或在评论中与我联系。最好的祝愿 摘要: