3.7 nodejs发布package

本文详细介绍了使用Node.js创建、构建、测试、发布项目的全过程,包括如何使用package.json进行项目配置,创建test.js和command.js文件进行单元测试和命令执行,以及如何通过npm进行本地测试、登录、发布等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、新建个项目wxwcd-test-publish

//package.json

{
  "name": "wxwcd-test-publish",
  "version": "1.0.0",
  "description": "",
  "main": "src/test.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "run": "src/command.js"
  },
  "files": [
    "src/"
  ],
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "figlet": "^1.5.2"
  }
}

二、新建test.js

class Test{
    constructor(name,age) {
        this.name = name ;
        this.age = age;
    }

    get(){
        return this.name + ':' + this.age;
    }

    set(name ,age){
        this.name = name;
        this.age = age
    }
}

module.exports = Test

三、再新建个command.js,以便执行bin命令

#!/usr/bin/env node
//#!/usr/bin/env node这段代码是必须要的
const figlet = require('figlet')
figlet('Hello World!!', function(err, data) {
    if (err) {
        return;
    }
    console.log(data)
});

四、执行npm link建立软链,在本地试试bin命令是否正常,执行bin中的run

 五、执行npm login登录后,再执行npm publish

六、新建个项目试试引入刚刚这个包

npm install wxwcd-test-publish@latest

七、新建test.js

最后也是执行成功的

const T = require('wxwcd-test-publish')

let t = new T('wu',20);
t.set('cai', 22);
console.log(t.get())

 八、试试bin命令,控制台输入“run”,也是执行成功的

 九,如果要发布新的版本,只需要改变版本号即可,通过repository字段可以配置源码地址、

{
  "name": "wxwcd-test-publish",
  "version": "1.0.3",
  "description": "",
  "main": "src/test.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "run": "src/command.js"
  },
  "files": [
    "src/"
  ],
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "figlet": "^1.5.2"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/test.js.git\""
  }
}

npm ERR! code 1 npm ERR! path D:\work\opscloud4\opscloud4-web-master\node_modules\deasync npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node ./build.js npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@10.0.1 npm ERR! gyp info using node@18.19.0 | win32 | x64 npm ERR! gyp info find Python using Python version 3.12.3 found at "C:\Users\27145\AppData\Local\Programs\Python\Python312\python.exe" npm ERR! gyp ERR! find VS npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details npm ERR! gyp ERR! find VS looking for Visual Studio 2015 npm ERR! gyp ERR! find VS - not found npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8 npm ERR! gyp ERR! find VS npm ERR! gyp ERR! find VS ************************************************************** npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload. npm ERR! gyp ERR! find VS For more information consult the documentation at: npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows npm ERR! gyp ERR! find VS ************************************************************** npm ERR! gyp ERR! find VS npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use npm ERR! gyp ERR! stack at VisualStudioFinder.fail (D:\ProgramFile\developTool\nvm\nvm\v18.19.0\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:113:11) npm ERR! gyp ERR! stack at VisualStudioFinder.findVisualStudio (D:\ProgramFile\developTool\nvm\nvm\v18.19.0\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:69:17) npm ERR! gyp ERR! stack at process.processTicksAndRejections (node:internal/process/task_queues:95:5) npm ERR! gyp ERR! stack at async createBuildDir (D:\ProgramFile\developTool\nvm\nvm\v18.19.0\node_modules\npm\node_modules\node-gyp\lib\configure.js:69:26) npm ERR! gyp ERR! stack at async run (D:\ProgramFile\developTool\nvm\nvm\v18.19.0\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js:81:18) npm ERR! gyp ERR! System Windows_NT 10.0.26100 npm ERR! gyp ERR! command "D:\\ProgramFile\\developTool\\nvm_nodejs\\nodejs\\node.exe" "D:\\ProgramFile\\developTool\\nvm\\nvm\\v18.19.0\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" npm ERR! gyp ERR! cwd D:\work\opscloud4\opscloud4-web-master\node_modules\deasync npm ERR! gyp ERR! node -v v18.19.0 npm ERR! gyp ERR! node-gyp -v v10.0.1 npm ERR! gyp ERR! not ok npm ERR! Build failed
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值