返回
Featured image of post ArchLinux 安装配置 Node.js

ArchLinux 安装配置 Node.js

安装 Node.js 并配置目录

安装 Node.js 以及 NPM

ArchLinux 的 Node.js 安装比较简单

sudo pacman -S nodejs npm

配置软件包安装目录

Node.js 的安装还是十分简单的,但是在安装软件包的时候会出现权限问题,手动安装到/opt目录则不会

$ npm i -g nali-cli
...
npm ERR! Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/nali-cli'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/nali-cli'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/lib/node_modules/nali-cli'
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.
...

提示权限不足,让我们以管理员身份运行

解决方法: 配置新的安装目录(用户有读写权限)

  1. 创建目录用于安装软件包: mkdir ~/.npm-global
  2. 配置 NPM 使其使用新目录: npm config set prefix '~/.npm-global'
  3. 将新目录添加到 PATH 变量: echo 'export PATH=$PATH:~/.npm-global/bin' >> ~/.profile
  4. 注销重新登录,或使用 source 指令使其在当前终端生效: source ~/.profile

注意: 此方法仅对单用户有效,若要全局有效请将 ~/.npm-global 换成所有用户都有读写权限的绝对路径,并将环境变量写入 /etc/profile 而不是 ~/.profile


安装 yarn (可选)

Yarn 是新一代的 npm 包管理器,由 Facebook 开发

npm i -g yarn

基础使用方法:

  1. 安装软件包: yarn add <package>
  2. 全局安装: yarn global add <package>
  3. 删除软件包: yarn remove <package>
  4. 根据 package.json 自动安装: yarn

解决包下载慢以及下载失败的问题

方法一: 修改 NPM 源

在安装软件的时候临时使用:

npm install <package> --registry=https://registry.npm.taobao.org

写入配置文件永久使用:

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

方法二: 修改 Yarn 源

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

方法三: 使用 cnpm

cnpm 相当于默认使用 ali 源的 npm,安装:

npm install cnpm --registry=https://registry.npm.taobao.org

其他软件包的加速源

将下列字段写入 ~/.npmrc 或者 ~/.yarnrc (yarn 会继承 npm 的设置)

  • ChromeDriver
chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver
  • Electron
electron_mirror=https://npm.taobao.org/mirrors/electron
  • 其他
disturl=https://npm.taobao.org/dist
couchbase-binary-host-mirror=https://npm.taobao.org/mirrors/couchbase/v{version}
debug-binary-host-mirror=https://npm.taobao.org/mirrors/node-inspector
flow-bin-binary-host-mirror=https://npm.taobao.org/mirrors/flow/v
fse-binary-host-mirror=https://npm.taobao.org/mirrors/fsevents
fuse-bindings-binary-host-mirror=https://npm.taobao.org/mirrors/fuse-bindings/v{version}
git4win-mirror=https://npm.taobao.org/mirrors/git-for-windows
gl-binary-host-mirror=https://npm.taobao.org/mirrors/gl/v{version}
grpc-node-binary-host-mirror=https://npm.taobao.org/mirrors
hackrf-binary-host-mirror=https://npm.taobao.org/mirrors/hackrf/v{version}
leveldown-binary-host-mirror=https://npm.taobao.org/mirrors/leveldown/v{version}
leveldown-hyper-binary-host-mirror=https://npm.taobao.org/mirrors/leveldown-hyper/v{version}
mknod-binary-host-mirror=https://npm.taobao.org/mirrors/mknod/v{version}
node-sqlite3-binary-host-mirror=https://npm.taobao.org/mirrors
node-tk5-binary-host-mirror=https://npm.taobao.org/mirrors/node-tk5/v{version}
nodegit-binary-host-mirror=https://npm.taobao.org/mirrors/nodegit/v{version}/
operadriver-cdnurl=https://npm.taobao.org/mirrors/operadriver
phantomjs-cdnurl=https://npm.taobao.org/mirrors/phantomjs
profiler-binary-host-mirror=https://npm.taobao.org/mirrors/node-inspector/
puppeteer-download-host=https://npm.taobao.org/mirrors
python-mirror=https://npm.taobao.org/mirrors/python
rabin-binary-host-mirror=https://npm.taobao.org/mirrors/rabin/v{version}
sass-binary-site=https://npm.taobao.org/mirrors/node-sass
sodium-prebuilt-binary-host-mirror=https://npm.taobao.org/mirrors/sodium-prebuilt/v{version}
sqlite3-binary-site=https://npm.taobao.org/mirrors/sqlite3
utf-8-validate-binary-host-mirror=https://npm.taobao.org/mirrors/utf-8-validate/v{version}
utp-native-binary-host-mirror=https://npm.taobao.org/mirrors/utp-native/v{version}
zmq-prebuilt-binary-host-mirror=https://npm.taobao.org/mirrors/zmq-prebuilt/v{version}

更多请参考: Mirrors - taobao.org

Built with Hugo
Theme Stack designed by Jimmy