Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

教你如何发布一个自己的npm包 #11

Open
shen1992 opened this issue Feb 7, 2018 · 0 comments
Open

教你如何发布一个自己的npm包 #11

shen1992 opened this issue Feb 7, 2018 · 0 comments

Comments

@shen1992
Copy link
Owner

shen1992 commented Feb 7, 2018

前言

最近做了一个组件,想发布到npm上,遇到了一些问题,所以记录一下心路历程。

1.确保自己是npm源

如果你更换了淘宝源,请换回来,否则发布的时候会一直报404。

查看使用源

npm config get registry

使用npm源

npm config set registry https://registry.npmjs.org/

2.项目结构

主要的文件放在lib文件夹下

./lib/index.js

export default CountDonw { ...}

./index.js是整个项目的入口文件

module.exports = require(./lib)

3.webpack打包问题

我希望组件不仅能通过

npm i CountDown --save
import CountDown from 'CountDown'

的方式使用,而且还能直接把dist.js下下载,通过script的方式使用,这就需要配置一下webpack

./webpack.config.js

output: {
  library: 'CountDown',
  libraryExport: 'default'
}

这样配置就改变了webpack的打包方式,可以通过script,然后new CountDonw(...)使用了。

4.注册

官网上注册一个账号,然后把账号加入注册表

npm adduser

查看当前的用户

npm whoami

发布

执行

npm publish 

5.更新

更改一下package.json里面的version,然后执行

npm publish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant