当前位置 博文首页 > 纸飞机博客:??npm常用命令以及npm publish常见问题处理方法??

    纸飞机博客:??npm常用命令以及npm publish常见问题处理方法??

    作者:[db:作者] 时间:2021-09-11 16:46

    目录

    💕一、npm常用命令

    🔥1、安装配置命令

    🔥2、npm init创建初始化

    🔥3、npm set 设置环境变量

    🔥4、npm search 搜索模块?

    🔥5、npm list 查看模块

    🔥6、npm install 安装模块

    🔥7、npm uninstall 卸载模块?

    🔥8、npm update 更新模块?

    🔥9、npm link 引用模块?

    🔥10、npm run 执行脚本?

    🔥11、npm publish 发布模块

    💕二、?npm publish常见问题

    🔥1、未登录

    ?🔥2、包名重复

    🔥3、邮箱未认证

    🔥4、检查仓库

    🔥5、仓储地址不对

    🔥往期优质文章推荐🔥


    💕一、npm常用命令

    🔥1、安装配置命令

    # 查看 npm 的版本 
    npm -v  //6.4.0 << 安装成功会返回版本号
    
    # 查看各个命令的简单用法
    npm -l 
     
    # 查看 npm 命令列表
    npm help
    
    # 查看 npm 的配置
    npm config list -l

    🔥2、npm init创建初始化

    npm init

    npm init用来初始化生成一个新的package.json文件。它会向用户提问一系列问题,如果觉得不用修改默认配置,一路回车就可以了。

    尾缀带-f(代表force)、-y(代表yes),则跳过提问阶段,直接生成一个新的package.json文件,不带尾缀的话,默认有提问阶段。

    🔥3、npm set 设置环境变量

    npm set init-author-name 'my name jerry'
    npm set init-author-email '12345@qq.com'
    npm set init-author-url 'http://yourdomain.com'
    npm set init-license 'MIT'
    #执行了以上的修改,此时 Package.json并没有发生变化
    
    #设置后执行init才是真正修改成功
    npm init

    🔥4、npm search 搜索模块?

    npm search <搜索词> [-g]

    npm search命令用于搜索npm仓库,它后面可以跟字符串,也可以跟正则表达式。

    🔥5、npm list 查看模块

    #当前项目安装的所有模块
    npm list
    
    #列出全局安装的模块 带上[--depth 0] 不深入到包的支点 更简洁
    npm list -g --depth 0

    🔥6、npm install 安装模块

    # 读取package.json里面的配置单安装  
    npm install 
    # 可简写成 npm i
    
    # 默认安装指定模块的最新(@latest)版本
    npm install [<@scope>/]<name> 
    # eg:npm install vue
    
    # 安装指定模块的指定版本
    npm install [<@scope>/]<name>@<version>
    # eg: npm install vue@2.0.1
    
    # 安装指定指定版本范围内的模块
    npm install [<@scope>/]<name>@<version range>
    # eg: npm install vue@">=1.0.28 < 2.0.0"
    
    # 安装指定模块的指定标签 默认值为(@latest)
    npm install [<@scope>/]<name>@<tag>
    # eg:npm install sax@0.1.1
    
    # 通过Github代码库地址安装
    npm install <tarball url>
    # eg:npm install git://github.com/package/path.git

    🔥7、npm uninstall 卸载模块?

    #卸载当前项目或全局模块 
    npm uninstall <name> [-g] 
    #eg: npm uninstall gulp --save-dev 
    #    npm uninstall gulp -g
    
    #卸载后,你可以到 /node\_modules/ 目录下查看包是否还存在,或者使用以下命令查看:
    npm ls # 查看安装的模块

    🔥8、npm update 更新模块?

    #升级当前项目或全局的指定模块
    npm update <name> [-g] 
    # eg: npm update express 
    #     npm update express -g

    🔥9、npm link 引用模块?

    # 引用依赖 有些包是全局安装了,在项目里面只需要引用即可。
    npm link [<@scope>/]<pkg>[@<version>]
    # eg: 引用   npm link gulp gulp-ssh gulp-ftp
    # eg: 解除引用 npm unlink gulp

    🔥10、npm run 执行脚本?

    package.jsonscripts字段,可以用于指定脚本命令,供npm直接调用。npm run会创建一个Shell,执行指定的命令。

    🔥11、npm publish 发布模块

    # 未注册 申请注册一个用户 直接在https://www.npmjs.com/注册一样
    npm adduser
    # 执行后 填写几个问题 Username、Password、Email
    
    #已注册
    npm login 
    
    #登录
    npm login
    
    #查询登录账号
    npm whoami
    
    #发布
    npm publish

    💕二、?npm publish常见问题

    🔥1、未登录

    npm ERR! code ENEEDAUTH

    npm ERR! need auth auth required for publishing

    npm ERR! need auth You need to authorize this machine using `npm adduser`

    解决:npm login 或者添加用户?npm adduser

    ?🔥2、包名重复

    npm ERR! publish Failed PUT 403

    npm ERR! code E403

    npm ERR! You do not have permission to publish "unit". Are you logged in as the correct user? : unit

    解决:将package.json中的name替换一个特殊点的名字。?

    🔥3、邮箱未认证

    npm ERR! publish Failed PUT 403

    npm ERR! code E403

    npm ERR! Forbidden : unit-dingding(我的包名)

    解决:去邮箱验证npm发来的验证邮箱邮件。??

    🔥4、检查仓库

    npm ERR! publishFailedPUT403

    npm ERR! codeE403

    npm ERR! no_permsPrivatemode enable,only admin can publish this module:...

    解决:切换npm源

    npm config set registry=https://registry.npm.taobao.org/

    🔥5、仓储地址不对

    npm ERR!?code E409

    npm ERR!?Registry returned 409 for PUT on?http://r.cnpmjs.org/-/user/or...:dingding: conflict

    通过nrm ls?命令查看我此时的仓库地址为cnpm,而不是npm

    解决:用nrm切换到npm仓库,执行命令nrm use npm。?

    问题解决后再次执行发布命令npm publish,发布成功:

    包发布成功

    完结撒花。


    ?参考:

    https://www.jianshu.com/p/47b1ef1d4a2c

    https://zhuanlan.zhihu.com/p/122224879?

    🔥往期优质文章推荐🔥

    https://blog.csdn.net/qq_32442973/article/details/120144460

    https://blog.csdn.net/qq_32442973/article/details/120069291

    https://blog.csdn.net/qq_32442973/article/details/120004973

    https://blog.csdn.net/qq_32442973/article/details/119984737

    cs
    下一篇:没有了