npm
npm命令
npm官网
https://www.npmjs.com/
npm官方地址
npm config set registry https://registry.npmjs.org/
npm淘宝镜像地址
npm config set registry https://registry.npm.taobao.org
查看npm源地址
npm config get registry
npm install指定版本
npm install gulp@4.0.0 -g
npm 删除(废弃)发布包或版本
npm unpublish dzmtest@1.0.1 --force
npm unpublish dzmtest --force
nrm
说明:nrm 是一个管理 npm 镜像地址的工具,可以通过它来改变 npm 镜像源地址
- 安装
npm i -g nrm
- 查看可用的npm源地址
nrm ls
- 设置npm地址
nrm use npm
npm发布包
- 使用npm 创建一个项目
npm init
- 编写组件
<template> <div> 计算器 </div> </template> <script> export default { methods:{ add(n,m){ console.log('求和', n+m) return n+m } } } </script> <style> </style>
- app.vue调用,方便调试
<template> <div id="app"> <npmtest/> </div> </template> <script> import npmtest from './components/npmtestlijingjing.vue' export default { name: 'app', data () { return { } }, components:{ npmtest } } </script> <style> </style>
- index.js
// 方便vue项目引入 import npmtestlijingjing from './src/components/npmtestlijingjing' import _Vue from 'vue' npmtestlijingjing.install = Vue => { if (!Vue) { window.Vue = Vue = _Vue } Vue.component(npmtestlijingjing.name, npmtestlijingjing) } export default npmtestlijingjing;
- webpack.config.js
var path = require('path') var webpack = require('webpack') const NODE_ENV = process.env.NODE_ENV module.exports = { // entry: './src/main.js', entry:NODE_ENV=='development'? './src/main.js':'./index.js', output: { path: path.resolve(__dirname, './dist'), publicPath: '/dist/', // filename: 'build.js' filename:'npmtestlijingjing.js', library:'npmtestlijingjing', libraryTarget:'umd', umdNamedDefine:true },
- package.json
"name": "npmtestlijingjing", "description": "test", "version": "1.0.2", "author": "lijingjingw", "license": "y", "private": false, "main": "./dist/npmtestlijingjing.js",
- index.html
<script src="/dist/npmtestlijingjing.js"></script>
- 打包命令
npm run build
npm publish
- 调用自己发布的包
npm install npmtestlijingjing@1.0.2
import npmtestlijingjing from 'npmtestlijingjing'
console.log('npmtestlijingjing', npmtestlijingjing.methods.add(1, 3))
- 查看自己发布的包