NPM and Plugin Guide


版本

  • NPM

package.json

peerDependency

https://docs.npmjs.com/cli/v6/configuring-npm/package-json#peerdependencies

In some cases, you want to express the compatibility of your package with a host tool or library, while not necessarily doing a require of this host. This is usually referred to as a plugin. Notably, your module may be exposing a specific interface, expected and specified by the host documentation.

通常是在插件开发的场景下,你的插件需要某些依赖的支持,但是你又没必要去安装,因为插件的宿主会去安装这些依赖,你就可以用peerDependencies去声明一下需要依赖的插件和版本,如果出问题npm就会有警告来提醒使用者去解决版本冲突问题。(翻译出处之一)

NPM 日常使用

新建项目

# Commands tested on windows
mkdir projectName
cd projectName

# 1. 根据提示输入,一步一步创建 package.json;
npm init

# 2. 直接初始化一个默认的 package.json (y = yes)
npm init -y

mkdir src
cd src
echo off > index.js

常用命令

基本用法

  • 安装包,查看包信息

  • Install webpack webpack-cli

  • Publish NPM package

  • NPM Link: Test packages before publish

  • Config

  • 场景示例

问题列表

  • npm ERR! Unexpected end of JSON input while parsing near '....0.0","inherits":"^2.'

The problem is with the spaces in URL(C:\Program Files). Try to install the nvm directly in C:/ or in other folder without spaces in the PATH.

  • 发布 NPM 包版本时: npm ERR! 403 403 Forbidden - PUT * - [no_perms] Private mode enable, only admin can publish this module

registry 配置项被修改为非 npmjs, 需要重新设置为 npmjs

  • Git set custom registry when group is using cnpm

  • npm ERR! code E401 npm ERR! 401 Unauthorized - PUT https://xxx - [unauthorized] Login first

可能是 cnpm 和 npm 设置 registry 的变更导致已登录账号的信息在另外的环境上,暂时是通过修改账号名的方式解决。 npm ERR! 409 Conflict - PUT https://[cnpm]/-/user/org.couchdb.user:[username] - [conflict] User [username] already exists · Issue #1607 · cnpm/cnpmjs.org

  • npm ERR! cb() never called!

  • TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

throw new ERR_INVALID_ARG_TYPE(name, 'string', value);

  • npm i -g whistle 后 w2 start 的 error

  • npm install gyp ERR! stack Error: EACCES: permission denied, mkdir ...

  • could not resolve dependency: npm err! peer react@"^16.8.0 || ^17.0.0" from @xxxx

  • requires a peer of react@^16.8.0 but none is installed. You must install peer dependencies yourself.

NVM: Node version manager

镜像设置

在使用nvm安装node的过程中如果速度太慢的话,可以设置改用淘宝的镜像地址

在nvm安装目录下的setting.txt文件中加入以下代码

安装 NVM

Windows

coreybutler/nvm-windows: A node.js version management utility for Windows. Ironically written in Go.

  1. Uninstall all node;

  2. Download and install nvm; (windows: nvm-setup.zip) nvm-windows

  3. Check and find usage;

Mac

macOS 上使用 brew 安装 NVM 管理 node.js

参考

最后更新于

这有帮助吗?