若是你已經註冊過npm包,能夠跳過去。
輸入命令 npm adduser
根據提示輸入 name、password 、email。
輸完以後,記得去剛剛登記的郵箱激活一下,否則會沒法發佈哦。webpack
tips:web
npm config set registry https://registry.npmjs.org/
npm whoami
查看。我是參考 webpack 建立library 寫的。
簡單來講就是:npm
// index.jsjson
import _ from 'lodash'; import numRef from './ref.json'; export function numToWord(num) { return _.reduce(numRef, (accum, ref) => { return ref.num === num ? ref.word : accum; }, ''); }; export function wordToNum(word) { return _.reduce(numRef, (accum, ref) => { return ref.word === word && word.toLowerCase() ? ref.num : accum; }, -1); };
// ref.jsonide
[{ "num": 1, "word": "One" }, { "num": 2, "word": "Two" }, { "num": 3, "word": "Three" }, { "num": 4, "word": "Four" }, { "num": 5, "word": "Five" }, { "num": 0, "word": "Zero" }]
var path = require('path'); module.exports = { entry: { index: './src/index.js' }, output: { path: path.resolve(__dirname, 'dist'), filename: 'webpack-numbers.js', library: 'webpackNumbers', libraryTarget: 'umd' }, externals: { lodash: { commonjs: 'lodash', commonjs2: 'lodash', amd: 'lodash', root: '_' } } }
// scripts 部分函數
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "webpack --config webpack.config.js" },
執行
npm run build
npm publishui
發佈以後能夠在 https://www.npmjs.com/ 查找本身發佈的包。spa
在其餘項目,或者另外的項目npm install xxx
// xxx 是你剛剛發佈的包名字
引入3d
import { numToWord } from 'webpack-number-hqy';
而後就能夠使用 numToWord 函數拉code