npm init 解析

npm 快速初始化:

1
npm init -f (--force or --yes)

将会使用默认值配置;

如何修改默认值?

1
2
3
4
5
npm config set init.author.email "lhongda@live.com"
npm config set init.author.name "anderson"
npm config set init.author.url "http://github.com/peacesky"
npm config set init.license "MIT"
npm config set init.version "0.1.0"

npm run 的实际执行过程:

  1. 从 package.json 文件中读取 scripts 对象里面的全部配置;
  2. 以传给 npm run 的第一个参数作为键,本例中为 xxx,在 scripts 对象里面获取对应的值作为接下来要执行的命令,如果没找到直接报错;
  3. 在系统默认的 shell 中执行上述命令,系统默认 shell 通常是 bash,windows 环境下可能略有不同.

上下文的 eslint 命令从哪里来

其实,npm 在执行指定 script 之前会把 node_modules/.bin 加到环境变量 $PATH 的前面,这意味着任何内含可执行文件的 npm 依赖都可以在 npm script 中直接调用,换句话说,你不需要在 npm script 中加上可执行文件的完整路径,比如 ./node_modules/.bin/eslint **.js .

Cli 初始化 eslint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  npm-custom-init git:(master) ✗ npm install eslint -D
➜ npm-custom-init git:(master) ✗ ./node_modules/.bin/eslint --init
? How would you like to configure ESLint? Answer questions about your style
? Are you using ECMAScript 6 features? Yes
? Are you using ES6 modules? Yes
? Where will your code run? Browser
? Do you use CommonJS? No
? Do you use JSX? No
? What style of indentation do you use? Tabs
? What quotes do you use for strings? Single
? What line endings do you use? Unix
? Do you require semicolons? Yes
? What format do you want your config file to be in? JavaScript
Successfully created .eslintrc.js file in /Users/anderson/Code/lang-experiments/js/npm/npm-custom-init

配置 Vue/React eslint

1
2
https://github.com/vuejs/eslint-plugin-vue
https://github.com/yannickcr/eslint-plugin-react