Henry Henry
  • JavaScript
  • TypeScript
  • Vue
  • ElementUI
  • React
  • HTML
  • CSS
  • 技术文档
  • GitHub 技巧
  • Nodejs
  • Chrome
  • VSCode
  • Other
  • Mac
  • Windows
  • Linux
  • Vim
  • VSCode
  • Chrome
  • iTerm
  • Mac
  • Obsidian
  • lazygit
  • Vim 技巧
  • 分类
  • 标签
  • 归档
  • 网站
  • 资源
  • Vue 资源
GitHub (opens new window)

Henry

小学生中的前端大佬
  • JavaScript
  • TypeScript
  • Vue
  • ElementUI
  • React
  • HTML
  • CSS
  • 技术文档
  • GitHub 技巧
  • Nodejs
  • Chrome
  • VSCode
  • Other
  • Mac
  • Windows
  • Linux
  • Vim
  • VSCode
  • Chrome
  • iTerm
  • Mac
  • Obsidian
  • lazygit
  • Vim 技巧
  • 分类
  • 标签
  • 归档
  • 网站
  • 资源
  • Vue 资源
GitHub (opens new window)
  • 技术文档

  • GitHub

  • Nodejs

    • 打造属于自己的项目脚手架工具
    • node 和 npm 常见问题
      • npm 配置淘宝镜像:
      • node-sass 安装失败的解决方法:
      • Missing write access to /usr/local/lib/node_modules
        • 参考资料
    • node 和 npm 简介
    • 手搓一个 TinyPng 压缩图片的脚手架
    • node 监听文件夹并处理
    • 相对路径转别名路径之 chokidar
  • Chrome

  • VSCode

  • VSCode 更新文档

  • Other

  • 技术
  • Nodejs
Henry
2017-08-04
目录

node 和 npm 常见问题

记录 node 和 npm 常见问题

# npm 配置淘宝镜像:

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

# node-sass 安装失败的解决方法:

  • 设置全局镜像源: npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
  • 参考资料: 整理 node-sass 安装失败的原因及解决办法 - - SegmentFault 思否 (opens new window)

# Missing write access to /usr/local/lib/node_modules

解决方法有很多种, 可以参考这里: node.js - npm install -g less does not work: EACCES: permission denied - Stack Overflow (opens new window)

这里主要说一下以下这种解决方法的问题:

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder.

Make a directory for global installations:

mkdir ~/.npm-global
1

Configure npm to use the new directory path:

npm config set prefix '~/.npm-global'
1

Open or create a ~/.profile file and add this line:

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

Back on the command line, update your system variables:

source ~/.profile
1

Test: Download a package globally without using sudo.

npm install -g jshint
1

If still show permission error run (mac os):

sudo chown -R $USER ~/.npm-global
1

由于我是使用 iTerm2 命令行工具的, shell 使用的是 zsh , 当使用 vi ~/.profile 添加环境变量 export PATH=~/.npm-global/bin:$PATH 后, 原始系统命令(如 ls , vi )均无法使用

查询后才知道:

原因是因为环境变量的问题, 编辑 profile 文件没有写正确, 导致在命令行下 ls 等命令不能够识别.

解决办法: 在命令行下打入下面这段就可以了

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

所以我这种情况是不能编辑 ~/.profile 文件的

需要编辑的是 ~/.zshrc 文件, 在该文件种添加环境变量 export PATH=~/.npm-global/bin:$PATH 就可以了

zsh 的配置文件不再是 /.zsh_profile 去调用 /.zshrc,而是直接就是 /.zshrc 就可以了.所以对于 zsh 的一切设置,直接去 /.zshrc 中设置.

# 参考资料

  • node.js - npm install -g less does not work: EACCES: permission denied - Stack Overflow (opens new window)
  • ls command not found 的解决办法 - 知乎 (opens new window)
  • ~/.profile ~/.bashrc和~./bash_profile的理解以及zsh的使用 - 简书 (opens new window)
编辑 (opens new window)
#node#npm
上次更新: 5/27/2023, 1:02:05 PM
打造属于自己的项目脚手架工具
node 和 npm 简介

← 打造属于自己的项目脚手架工具 node 和 npm 简介→

最近更新
01
version 1.15
07-01
02
version 1.14
06-27
03
version 1.13
06-27
更多文章>
Theme by Vdoing | Copyright © 2017-2023 HenryTSZ | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式