[TOC]
# npm script
npm script 和 webpack 就可以做很多事情了,還可以自己寫npm script腳本,來完成自己想做的事情。
## 參考
[ 我為何放棄Gulp與Grunt,轉投npm scripts(上)](http://blog.csdn.net/ricohzhanglong/article/details/50726141)
http://www.cnblogs.com/zldream1106/p/5204599.html
[使用npm scripts替代gulp](https://aotu.io/notes/2016/02/26/use-npm-script-instead-of-gulp/)
[npm Scripts使用教程【譯】](http://www.cnblogs.com/zldream1106/p/why-npm-scripts.html)
> https://docs.npmjs.com/misc/scripts
# set-script
示例:
```shell
npm set-script start "http-server ."
```
`package.json`:
```json
{
"name": "my-project",
"scripts": {
"start": "http-server .",
"test": "some existing value"
}
}
```
> [npm-set-script](https://docs.npmjs.com/cli/v8/commands/npm-set-script)
# npm-dist-tag
> version 一旦發布,是不可變的;
> 而 tag 更像一個渠道,只要用戶選擇了這個渠道,就可以一直更新這個渠道的最新版。
也因此,version 和 tag 是相互獨立的。
## 切換tag
回到我們的問題,如果不小心把`1.0.0-alpha.1`直接`npm publish`了,我們該怎么辦?
## 查看當前的tag和對應的version。
通過`npm dist-tag ls`,我們可以得到這樣的結果:
~~~css
latest: 1.0.0
next: 1.0.0-alpha.0
~~~
如果我們不小心發布了一個`1.0.0-alpha.1`到`latest`,那么我們會得到:
~~~css
latest: 1.0.0-alpha.1
next: 1.0.0-alpha.0
~~~
這個時候我們只要切一下 tag 就可以挽回局面了。
## 查看發布過的所有版本號
通過`npm v my-package versions`可以得到一個數組:
~~~json
[ '0.1.0',
'1.0.0',
'1.0.0-alpha.0',
'1.0.0-alpha.1' ]
~~~
## 設置 tag,對應到版本 version
```
npm dist-tag add my-package@version tag
```
如:
1. 把原來的`1.0.0`設置成最新的正式版
```sh
npm dist-tag add my-package@1.0.0 latest
```
2. 把`1.0.0-alpha.1`更新到最新的測試版
```sh
npm dist-tag add my-package@1.0.0-alpha.1 next
```
再也不用擔心搞出線上故障了~
> [npm-dist-tag](https://docs.npmjs.com/cli/v8/commands/npm-dist-tag)
> https://docs.npmjs.com/adding-dist-tags-to-packages
# `npm set`
設置 `package.json` 默認值
當初始化`package.json`可以執行
```
npm init -y
```
如果需要設置默認值:
```
npm set init.license "MIT"
npm set init.author.email "koopking@gmail.com"
npm set init.author.name "Yun.kou"
```
再來一遍
```
rm package.json
npm init -y
```
如果你想看默認值和編輯可以查看`~/.npmrc`
```
vim ~/.npmrc
```
# 更新 npm
在Windows上,更新 npm 比較麻煩。Windows 上的問題是 npm是與 node.js一起安裝的,
直接使用:
```
$ npm install --global npm@latest
```
下載的 npm 的新版本總是會被node 本身的版本所掩蓋。這是因為您的環境變量 `PATH` 中的條目的順序導致的問題。
簡單解決方案:npm-windows-upgrade (https:/?/github.?com/?felixrieseberg/?npm-?windows-?upgrade)
使用管理員權限 運行以下命令:
```
$ npm install -g npm-windows-upgrade
$ npm-windows-upgrade
```
check out the official release notes: https:/?/
github.?com/?npm/?cli/
# `.npmrc` file
There are a few different points here:
1. Where is the`.npmrc`file created.
2. How can you download private packages
Running`npm config ls -l`will show you all the implicit settings for npm, including what it thinks is the right place to put the`.npmrc`. But if you have never logged in (using`npm login`) it will be empty. Simply log in to create it.
Another thing is #2. You can actually do that by putting a`.npmrc`file in the NPM package's root. It will then be used by NPM when authenticating. It also supports variable interpolation from your shell so you could do stuff like this:
~~~
; Get the auth token to use for fetching private packages from our private scope
; see http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
; and also https://docs.npmjs.com/files/npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
~~~
**Pointers**
* [http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules](http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules)
* [https://docs.npmjs.com/files/npmrc](https://docs.npmjs.com/files/npmrc)
# [`node-gyp`](https://github.com/TooTallNate/node-gyp) need Visual C++ runtime libraries
The way to resolve this is to install Visual Studio. At the time of this writing (Feb 2015) the compilation works fine with [Visual Studio 2013 Update 4](http://www.visualstudio.com/en-us/news/vs2013-update4-rc-vs.aspx). You can then tell npm which version of Visual Studio you are using by the following command:
```shell
$ npm install -g browser-sync --msvs_version=2013
```
After installation of Visual Studio npm should not throw errors any longer, but only issue warnings. As far as we can tell, they do not impact Browsersync.
**正確姿勢**
在項目目錄 執行,
macOS 系統直接運行下面的命令即可:
~~~
SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install?node-sass
~~~
windows 下的可以拆分成兩個命令
~~~
set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/
npm install node-sass
~~~
# node-sass 安裝失敗 解決辦法
https://segmentfault.com/a/1190000010984731
[node-sass 安裝失敗 win32-x64-57_binding.node](https://www.jianshu.com/p/145cc862ceec)
注意查看 [win32-x64-59_binding.node](https://github.com/sass/node-sass/releases/) 是否支持你的node版本
## 設置 node-sass 路徑
安裝 node-sass 的時候總是會各種不成功,~翻墻,解決一切問題~
**正確姿勢**
在項目目錄 執行,
macOS 系統直接運行下面的命令即可:
~~~
SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install?node-sass
~~~
windows 下的可以拆分成兩個命令
~~~
set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/
npm install node-sass
~~~
第一種:直接右鍵我的電腦--》屬性--》高級系統設置--》環境變量--》添加。
第二種:set XXX=文件路經 --》`set SASS_BINARY_PATH=D:\Applications\win32-x64-64_binding.node`
查看環境是否合適:`echo %SASS_BINARY_PATH%`
第三種:我們可以在`.npmrc`中直接設置它們的鏡像地址,添加`node-sass`的鏡像地址:
~~~
# .npmrc
SASS_BINARY_SITE=http://npm.taobao.org/mirrors/node-sass
~~~
## 然后安裝 node-sass 模塊
```
npm rebuild node-sass
或者
npm i node-sass -D --verbose
```
- 講解 Markdown
- 示例
- SVN
- Git筆記
- github 相關
- DESIGNER'S GUIDE TO DPI
- JS 模塊化
- CommonJS、AMD、CMD、UMD、ES6
- AMD
- RequrieJS
- r.js
- 模塊化打包
- 學習Chrome DevTools
- chrome://inspect
- Chrome DevTools 之 Elements
- Chrome DevTools 之 Console
- Chrome DevTools 之 Sources
- Chrome DevTools 之 Network
- Chrome DevTools 之 Memory
- Chrome DevTools 之 Performance
- Chrome DevTools 之 Resources
- Chrome DevTools 之 Security
- Chrome DevTools 之 Audits
- 技巧
- Node.js
- 基礎知識
- package.json 詳解
- corepack
- npm
- yarn
- pnpm
- yalc
- 庫處理
- Babel
- 相關庫
- 轉譯基礎
- 插件
- AST
- Rollup
- 基礎
- 插件
- Webpack
- 詳解配置
- 實現 loader
- webpack 進階
- plugin 用法
- 輔助工具
- 解答疑惑
- 開發工具集合
- 花樣百出的打包工具
- 紛雜的構建系統
- monorepo
- 前端工作流
- 爬蟲
- 測試篇
- 綜合
- Jest
- playwright
- Puppeteer
- cypress
- webdriverIO
- TestCafe
- 其他
- 工程開發
- gulp篇
- Building With Gulp
- Sass篇
- PostCSS篇
- combo服務
- 編碼規范檢查
- 前端優化
- 優化策略
- 高性能HTML5
- 瀏覽器端性能
- 前后端分離篇
- 分離部署
- API 文檔框架
- 項目開發環境
- 基于 JWT 的 Token 認證
- 扯皮時間
- 持續集成及后續服務
- 靜態服務器搭建
- mock與調試
- browserslist
- Project Starter
- Docker
- 文檔網站生成
- ddd