本地 npm 发包,遇到的问题

1,762 阅读3分钟

package.json文件中,设置main字段

  • 这个字段的值是你程序主入口模块的ID。如果其他用户需要你的包,当用户调用require() 或者 import 方法时,返回的就是这个模块的导出(exports

main.png

npmignore 文件

  • 把忽略的文件写在这里,因为默认情况下,npm 打包会根据 .gitignore 来忽略文件,如果发现存在 .npmignore 的话,则使用这个文件
  • 是不需要发布到npm的文件和文件夹,规则和.gitignore一样。如果你的项目底下有.gitignore但是没有.npmignore,那么会使用.gitignore里面的配置
  • 链接:juejin.cn/post/698840…
  • 链接:juejin.cn/post/684490…

首先你得有npm账号,注册一个

  • npm地址:www.npmjs.com/
  • 注册成功记得进邮箱激活,核实邮箱地址(乱点一通就对了)

npm.png

有了npm账号后,就可以后续的操作,(直接在 vscode 上敲命令行)

  • npm login

error1.png

它报的这个错误原因:因为当前我设置的是cnpm 登录到的是cnpm,所以需要切换回来

  • npm config get registry 它打印出来的是cnpm淘宝源 https://registry.npm.taobao.org/, 所以我们需要切回到npmjs源

npm 1.png

切换回 npmjs 源后,继续操作 npm login

  • npm login

tcl.png

错误信息
npm notice Log in on http://registry.npmjs.org/
npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or highEmail: (this IS public) 4xxxxx@qq.com
npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 
1.2 or higher. You are currently using plaintext http to connect. Please visit the GitHub blog for more information: https://g1.2 or highithub.blog/2021-08-23-npm-registry-deprecating-tls-1-0-tls-1-1/                                                               y-deprecati
npm notice Beginning October 4, 2021, all connections to the npm registry - including for package installation - must use TLS 1.2 or high
1.2 or higher. You are currently using plaintext http to connect. Please visit the GitHub blog for more information: https://gy-deprecatiithub.blog/2021-08-23-npm-registry-deprecating-tls-1-0-tls-1-1/
npm ERR! code E426
npm ERR! 426 Upgrade Required - PUT http://registry.npmjs.org/-/user/org.couchdb.user:karso.he

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\waxxxxx.he\AppData\Local\npm-cache\_logs\2021-11-16T03_59_23_302Z-debug.log
  • 问题原因:Warning about TLS 1.2 appears with Node v16.3.0 and npm v7.21.0
  • 链接:github.com/npm/tls-tes…
  • 我在根目录新建 .npmrc 文件 ,添加 registry=https://registry.npmjs.org
  • 检查 .npmrc 文件,将注册表项更改为 https 后,它可以工作,没报这个错

npmrc.png

上面设置好后,继续 npm login 别怂,就是干,然后再 npm publish

  • npm login :(需要输入账号,密码,邮箱)
  • 成功后,输入:npm publish

rr.png

轮到 npm publish 报错了

  • npm publish

publish.png

# npm publish 报403错误:403 Forbidden - PUT https://registry.npmjs.org/ocean_test_npm - Forbidden
  • 原因: 刚注册好npm账号,便立即发布第一个包时便会报出错误。因为注册好账号后,没有在注册的邮箱中核实邮箱地址 ,到邮箱中找到Npm官网发送的邮件,点击核实邮箱地址

发包成功后,记得把npm又重新设置回cnpm (你喜欢就好)

记住每改一次内容,发包之前,记得改下版本号,不然会报错!!!

1.png

woca ,我发到npm上的是源码。我想把build后的.js 文件发上去,但是install下来后import发现找不到,还在debug中

结语

前端react QQ群:788023830 ---- React/Redux - 地下老英雄

前端交流 QQ群:249620372 ---- FRONT-END-JS前端

(我们的宗旨是,为了加班,为了秃顶……,仰望大佬),希望小伙伴们加群一起学习