0x001 概述

本篇文章承接上文,记录的是一些使用过程中的疑惑

0x001 墙的原因使得包下载太慢

解决方案:使用淘宝cnpm,推荐使用cnpm,因为如果修改npm仓库,将会导致无法发布包。

$ npm install -g cnpm --registry=https://registry.npm.taobao.org
/usr/local/bin/cnpm -> /usr/local/lib/node_modules/cnpm/bin/cnpm
+ cnpm@5.1.1
updated 4 packages in 10.035s

$ cnpm install lodash

0x002 无法安装全局包,提示EACCES ,没有权限

解决方案:推荐第一种,简单!

  • 第一种:给全局包安装路径权限

    $ npm config get prefix
    /usr/local
    $ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
  • 第二种:修改全局包安装路径到有权限的地方

     $ mkdir ~/.npm-global
     $ npm config set prefix '~/.npm-global'
     $ export PATH=~/.npm-global/bin:$PATH
     $ source ~/.profile

0x003 全局包和本地包的区别以及如何抉择

本质上是没有差别的,只是一个装在了全局文件夹,一个装在了项目文件夹。全局文件夹中的包到处都可以引用,而装在项目文件夹中的包只有该项目才可以引用(其实都可以引用),以下做实验来演示

  • 新建文件夹0x007-local-global-diff1,0x008-local-global-diff2,0x009-local-global-diff3,0x010-local-global-diff4,并且使用命名空间做包名

$ mkdir 0x007-local-global-diff1
$ mkdir 0x008-local-global-diff2
$ mkdir 0x009-local-global-diff3
$ mkdir 0x010-local-global-diff4
  • 编写并发布@followwinter/0x007-local-global-diff1@1.0.0

$ cd 0x007-local-global-diff1
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (0x007-local-global-diff1) @followwinter/0x007-local-global-diff1
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /MY_PROJECT/PROJECT_OWN/NodeJS/npm/0x007-local-global-diff1/package.json:

{
  "name": "@followwinter/0x007-local-global-diff1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) 

$ vim index.js
// index.js
exports.printMsg = function() {
  console.log("This is a message from the @followwinter/0x007-local-global-diff1@1.0.0 package");
}
$ npm publish --access public
+ @followwinter/0x007-local-global-diff1@1.0.0
  1. 安装并引用@followwinter/0x007-local-global-diff1@1.0.0

$ cd  0x008-local-global-diff2
$ vim index.js
$ npm install @followwinter/0x007-local-global-diff1
npm WARN saveError ENOENT: no such file or directory, open '/MY_PROJECT/PROJECT_OWN/NodeJS/npm/0x008-local-global-diff2/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/MY_PROJECT/PROJECT_OWN/NodeJS/npm/0x008-local-global-diff2/package.json'
npm WARN 0x008-local-global-diff2 No description
npm WARN 0x008-local-global-diff2 No repository field.
npm WARN 0x008-local-global-diff2 No README data
npm WARN 0x008-local-global-diff2 No license field.

+ @followwinter/0x007-local-global-diff1@1.0.0
added 1 package in 2.361s

$ vim index.js
// index.js
var myModule = require('@followwinter/0x007-local-global-diff1');
console.log(myModule);
myModule.printMsg();

$ node index.js
{ printMsg: [Function] }
This is a message from the @followwinter/0x007-local-global-diff1@1.0.0 package
  • 更新@followwinter/0x007-local-global-diff1@1.0.0版本

$ cd 0x007-local-global-diff1/
$  vim index.js

// index.js
exports.printMsg = function() {
  console.log("This is a message from the @followwinter/0x007-local-global-diff1@1.0.2 package");
}

$ vim package.js
// package.js
{
  "name": "@followwinter/0x007-local-global-diff1",
  "version": "1.0.2",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

$ npm publish --access public
+ @followwinter/0x007-local-global-diff1@1.0.2
  • 全局安装新版本并引用

$ cd 0x009-local-global-diff3/
$ npm install @followwinter/0x007-local-global-diff1 -g
+ @followwinter/0x007-local-global-diff1@1.0.2
$ vim index.js
// index.js
var myModule = require('@followwinter/0x007-local-global-diff1');
console.log(myModule);
myModule.printMsg();

$ node index.js
{ printMsg: [Function] }
This is a message from the @followwinter/0x007-local-global-diff1@1.0.2 package
  • 运行0x008-local-global-diff2,验证全局和本地的引用顺序

$ cd 0x008-local-global-diff2
$ node index.js
This is a message from the @followwinter/0x007-local-global-diff1@1.0.0 package
  • 0x010引用0x008中的node_modules中的0x007

$ cd 0x010-local-global-diff4/
$ vim index.js

// index.js
var myModule = require('/MY_PROJECT/PROJECT_OWN/NodeJS/npm/0x008-local-global-diff2/node_modules/@followwinter/0x007-local-global-diff1');
console.log(myModule);
myModule.printMsg();

$ node index.js 
{ printMsg: [Function] }
This is a message from the @followwinter/0x007-local-global-diff1@1.0.0 package

所以包的查找顺序是

- 项目的`node_modules`文件夹
- 全局的`node_modules`文件夹

但是都可以找到就是,主要是查找路径和顺序的问题,而关于如何抉择存在哪呢?考虑的问题有一下几点

  • 是否在意全局文件的占用问题,全局文件不手动删除会一直存在,占用磁盘空间

  • 是否在多个项目使用不同版本?版本之间还有冲突?如果是这样,推荐还是保存在本地

  • 是否有命令行功能?如果有推荐安装在全局,因为安装本地调用需要调整调用路径,很麻烦

0x004 dependenciesdependencies-dev节点的区别以及如何抉择

区别:没有啥区别,只有语义上的区别,dependencies一般说的是项目运行需要的包,或者说需要打包进生产环境的包,比如jQuerylodash之类,而dependencies-dev一般说的是开发需要用的包,比如webpackgulp之类,在生产环境中不需要打包。
如何选择就如果上面所有,需要打包进生产环境就保存到dependencies,只是在开发或者打包的时候使用的就保存到dependencies-dev即可。

0x005 提示不能发布当前版本

npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You cannot publish over the previously published version 1.0.2. : @followwinter/0x007-local-global-diff1

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/FollowWinter/.npm/_logs/2017-09-10T14_01_59_753Z-debug.log

解决方案:不能发布已经发布的版本,修改一下版本号就可以了

0x006 想不到了,想到了再写

0x007 资源


followWinter
1.5k 声望82 粉丝

暂时没有