Node.js速查笔记

安装Node.js

下载地址

https://nodejs.org/zh-cn/download/


##Windows安装

image-20210216075648178

image-20210216080100906

这块是弹窗内容

Tools for Native Modules

Optionally install the tools necessary to compile native modules.

Some npm modules need to compiled from C/C++ when installing. If you want to be able to install such modules, some tools(Python 2 and Visual Studio Build Tools) need to be installed.

下面这段是个复选框

Automatically install the necessary tools. Note that this will also install Chocolatey. The script will pop-up in a new window after the installation copletes.

Alternatively, follow the instructions at https://github.com/nodejs/node-gyp#on-windows to install the dependencies yourself.

这是在告诉会给你自动安装2个工具:

构建工具(Python 2和Visual Studio构建工具)、Chocolatey

构建工具是因为一些npm模块需要使用C/C++编译,如果想要编译这些模块,则需要安装这个工具。如果不安装这个构建工具,在之后使用 npm 安装模块的时候,会报错:gyp ERR! find Python
如果见到这个错误,知道是因为没有安装 构建工具。

构建工具和 Chocolatey 都是必装的,如果现在没有安装,可以之后再手动安装,提示里已经给出了相关解释:https://github.com/nodejs/node-gyp#on-windows

该提示还告诉你 Chocolatey 会在后面弹出一个 cmd 来安装。

为了避免以后麻烦,这里强烈建议选择“Automatically install …” ,然后 Next…


##查看版本

node -v
npm -v

配置全局路径和缓存路径

新建 node_globalnode_cache 文件夹

image-20210216103902317

在cmd中更改路径

npm config set prefix "D:\env\nodejs\node_global"
npm config set cache "D:\env\nodejs\node_cache"

# 推荐修改nodejs\node_modules\npm 下 npmrc 文件
prefix=D:\ENV\nodejs\node_global
cache=D:\ENV\nodejs\node_cache

配置环境变量

新增 NODE_PATH 系统变量

image-20210216105157825

Path 中加入%NODE_PATH%%NODE_PATH%\node_global,删掉Path中关于nodejs多余的配置


修改淘宝镜像

使用cmd
npm config set registry https://registry.npmmirror.com

推荐修改 npmrc 文件
registry=https://registry.npmmirror.com

安装测试

# 查看 config 信息
npm config list ls -l

# 全局安装
npm install -g @vue/cli
# 查看版本
npm vue/cli -v

npm 命令

# 查看npm拥有的全部命令
npm -h
# 查看某一个npm命令的详细用法
npm <command> -h
# 安装
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
# 卸载
npm uninstall [<@scope>/]<pkg>[@<version>]
# 更新
npm update [-g] [<pkg>...]
# 查看安装的模块
npm ls [[<@scope>/]<pkg> ...]

FAQ

# 无法加载文件 X:\xxxx\xxx.ps1,因为在此系统上禁止运行脚本。
# PowerShell 中执行,策略更改,选择 A

set-ExecutionPolicy RemoteSigned

Node.js速查笔记
https://元气码农少女酱.我爱你/30d740f1c46b/
作者
元气码农少女酱
发布于
2023年5月2日
许可协议