【npm设置代理-解决npm网络连接error network失败问题】

创建vue项目出错

创建vue项目

npm create vue@latest

连接网络失败

npm error code ECONNRESET
npm error syscall read
npm error errno ECONNRESET
npm error network request to https://registry.npmjs.org/create-vue failed, reason: read ECONNRESET
npm error network This is a problem related to network connectivity.
npm error network In most cases you are behind a proxy or have bad network settings.
npm error network
npm error network If you are behind a proxy, please make sure that the
npm error network 'proxy' config is set properly.  See: 'npm help config'
npm error A complete log of this run can be found in: C:\Users\aisqu\AppData\Local\npm-cache\_logs\2024-11-21T07_49_10_663Z-debug-0.log

查看npm代理

npm config get proxy
npm config get https-proxy

设置npm代理

对于Clash Verge

npm config set proxy http://127.0.0.1:7897
npm config set https-proxy http://127.0.0.1:7897

对于v2rayN

npm config set proxy http://127.0.0.1:10809
npm config set https-proxy http://127.0.0.1:10809

自定义代理服务器

设置密码

npm config set proxy http://<username>:<password>@<proxyServerDomanNameOrIP>:<port>
npm config set https-proxy http://<username>:<password>@<proxyServerDomanNameOrIP>:<port>
  • <username>:用户名
  • <password>:密码
  • <proxyServerDomanNameOrIP>:代理服务器域名或者IP
  • <port>:端口

没有设置密码

npm config set proxy http://<proxyServerDomanNameOrIP>:<port>
npm config set https-proxy http://<proxyServerDomanNameOrIP>:<port>

删除代理

npm config delete proxy
npm config delete https-proxy

更换其他源

查看当前源

npm config get registry

返回默认的是官方提供的源

https://registry.npmjs.org/

更改 npm 源

将 npm 的源切换为淘宝的镜像源。以后,所有的 npm install 命令都将使用该源。

npm config set registry https://registry.npmmirror.com/

切换回官方源

npm config set registry https://registry.npmjs.org/

临时切换源

只想临时更改源而不影响全局配置,可以在执行 npm install 时通过 --registry 选项来指定源。

npm install <package-name> --registry=https://registry.npmmirror.com/

比如

npm install lodash --registry=https://registry.npmmirror.com/

临时更换源创建vue项目

原始命令

npm create vue@latest

创建 Vue 项目并使用淘宝镜像源

npm init vue@latest my-vue-app --registry=https://registry.npmmirror.com/

创建 React 项目并使用淘宝镜像源

npm create react-app my-react-app --registry=https://registry.npmmirror.com/

创建 Next.js 项目并使用淘宝镜像源

npm create next-app my-next-app --registry=https://registry.npmmirror.com/

创建 Express 项目并使用淘宝镜像源

npm create express-app my-express-app --registry=https://registry.npmmirror.com/

npm其他常用命令

查看 npm 版本

npm -v

查看node版本

node -v

初始化项目

npm init

安装依赖

npm install用来安装项目中的依赖,通常会自动根据 package.json 文件中的 dependencies 和 devDependencies 安装相应的库。

npm install

指定安装特定的包

安装 express

npm install express

卸载依赖

npm uninstall express

或者

npm remove

查看已安装的依赖

查看本地依赖

npm list

查看全局依赖

npm list -g

查看当前项目中已安装的依赖和可用的最新版本

npm outdated

更新依赖

更新项目中所有依赖包到符合 package.json 中版本范围的最新版本。

npm update

指定包名来更新某个特定的包

npm update express

全局安装包

使用 -g 标志来全局安装某个包,这样可以在任何地方运行这个包的命令。

npm install -g create-react-app

运行脚本

在 package.json 文件的 scripts 部分定义自定义脚本,然后通过 npm run <script-name> 来运行这些脚本。

"scripts": {
  "start": "node server.js",
  "dev": "nodemon server.js"
  "test": "jest"
}

启动服务器

npm run start

启动开发模式的构建和服务

npm run dev

运行测试

npm run test

查看 npm 配置

查看当前配置

npm config list

查看所有配置

npm config ls -l

清除缓存

清除 npm 缓存,可以解决一些安装包时遇到的问题,特别是在依赖安装失败时。

npm cache clean --force

发布包

npm login
npm publish

登录和退出 npm 账户

登录到 npm 账户

npm login

退出 npm 账户

npm logout
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ztiddler

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值