• 彻底解决全局安装权限问题


    报错EACCES: permission denied,具体信息如下:

    npm ERR! code EACCES
    npm ERR! syscall mkdir
    npm ERR! path /usr/local/lib/node_modules/@vue
    npm ERR! errno -13
    npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@vue'
    npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@vue'] {
    npm ERR!   errno: -13,
    npm ERR!   code: 'EACCES',
    npm ERR!   syscall: 'mkdir',
    npm ERR!   path: '/usr/local/lib/node_modules/@vue'
    npm ERR! }
    npm ERR!
    npm ERR! The operation was rejected by your operating system.
    npm ERR! It is likely you do not have the permissions to access this file as the current user
    npm ERR!
    npm ERR! If you believe this might be a permissions issue, please double-check the
    npm ERR! permissions of the file and its containing directories, or try running
    npm ERR! the command again as root/Administrator.
    
    npm ERR! A complete log of this run can be found in:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    虽然像这种权限问题加个sudo运行就可以顺利安装,但这绝非一个好的方法,因为当我之前毫不犹豫这样解决的时候遇到了很多问题。
    比如每次像这样的全局安装卸载等操作时都需要sudo
    其次因为这个文件是sudo安装的,因此文件权限就是root,之后对这个文件的操作更是会面临诸多问题。

    因此我尝试了新的解决方案,创建一个用户有权限的global目录。具体命令如下:

    cd ~
    mkdir .npm-global
    npm config set prefix '~/.npm-global'
    
    • 1
    • 2
    • 3

    更新path

    export PATH="~/.npm-global/bin:$PATH"
    
    • 1

    至此完美解决,此后不需要在每次全局安装都sudo

    具体原理是切到用户目录创建用户有权限的全局文件,
    将安装prefix目录设置为该目录。当不设置,将默认安装至node安装的目录,最后更新path

    在这里插入图片描述

    相关文档具体可参考:

    • https://www.npmjs.cn/files/folders/ (npm folder)
    • https://stackoverflow.com/questions/764600/how-can-you-export-your-bashrc-to-zshrc
    • https://www.cnblogs.com/tinywan/p/7224011.html
    • https://stackoverflow.com/questions/58269937/permission-denied-while-trying-to-install-vue-cli
  • 相关阅读:
    编译VTK静态库
    init container
    网络技术八:Vlan和Trunk基础
    Kotlin协程:受限协程作用域与序列发生器
    中国市场杂志社中国市场编辑部2022年第32期目录
    知识管理在业务中的价值如何体现
    2024腾讯一道笔试题--大小写字母移动
    机器学习西瓜书+南瓜书吃瓜教程学习笔记第四章决策树
    彩票系统java
    Vue3中插槽<slot>的概念和用法
  • 原文地址:https://blog.csdn.net/riddle1981/article/details/126732364