1. npm-shrinkwrap 類似yarn.lock固定包的依賴 以及包內(nèi)部的依賴
2. node-sass 在項目啟動時經(jīng)常報錯,很多時候運行npm rebuild node-sass就好了,官網(wǎng)對rebuild命令的解釋:
This command runs the npm build command on the matched folders. This is useful when you install a new version of node, and must recompile all your C++ addons with the new binary.
3.npm publish <package name>
在使用lerna publish的時候直接從0.1.0 跳到了 0.2.0 ,我們往往是很小版本的更新,可以手動更改version版本號,也可以使用npm version patch
更改最小的版本號
npm官網(wǎng)給的解釋
How to Publish & Update a Package
How to Update the Version Number
When you make changes, you can update the package using
npm version <update_type>
where <update_type> is one of the semantic versioning release types, patch, minor, or major.
$ lerna publish --cd-version (major | minor | patch | premajor | preminor | prepatch | prerelease)
# uses the next semantic version(s) value and this skips `Select a new version for...` prompt
手動更改packages下面的某個包的version publish只會publish這個包,并且lerna會在你更改的基礎(chǔ)上自動增加版本號
顯示package信息
npm info <package name>
npm publish --tag=beta
第一次npm publish --tag=beta
的時候verdaccio的控制界面是能看到的,再次發(fā)正式版verdaccio就看不到beta的版本號了,想完整查看發(fā)版信息的話就用npm info <package name>
4. npm link
在自己開發(fā)的node 命令項目下使用npm link 將其軟鏈接到全局執(zhí)行環(huán)境,從而在系統(tǒng)的任意路徑下都可以使用該cli工具。軟連接的一個非常好用的地方在于,你改了項目中的代碼,全局的cli也會跟著改變,很方便調(diào)試。
項目被軟連接到了C:\Program Files\nodejs目錄下,之前全局安裝的cli是被安裝到了C:\Users\bill\AppData\Roaming\npm下,不知道是不是更新了系統(tǒng)的原因,別人軟鏈使用 NPM 發(fā)布和使用 CLI 工具
是在C:\Users\bill\AppData\Roaming\npm目錄下的。
- 刪除軟鏈 npm unlink myModule
運行后項目的node_modules下的myModule軟鏈就會消失,但是C盤全局的目錄中的軟鏈并不會消失。
如果node項目是cli項目,那么直接在該項目下使用npm link,其他項目中就可以直接使用bin下的命令了
-
如果項目只是普通的項目不是cli項目,需要到使用該包的項目下運行npm link myModule,這樣myModule就會被軟鏈到該項目的node_modules目錄下,然后項目中就可以import 使用了
在項目下運行npm unlink update-iconfont
update-iconfont是項目名(name:“update-iconfont”)并不是cli命令的名字,link unlink都是項目名
如果想徹底刪除全局目錄下的軟鏈 需要npm rm --global myModule
使用npm ls --global myModule查看有沒有被刪除成功
參考:
How do I uninstall a package installed using npm link?
npm模塊管理器
package.json
{
"name": "update-iconfont",
"version": "0.0.1",
"description": "download or update iconfont",
"main": "index.js",
"bin": {
"udiconfont": "./bin/udicon.js"
},
"repository": "xxxx",
"author": "balibabu",
"license": "MIT",
"dependencies": {}
}
publish到npm后,使用npx udiconfont會報錯
使用npx update-iconfont就不會報錯。
5. git tag 和 npm version patch
糾結(jié)于升級package.json version的同時怎么同步更新git tag.嘗試在項目下運行npm version patch
,結(jié)果:
- package.json name更新了
- git add 并commit ,commit message就是版本號 0.0.2
- git tag 此刻自動更新為v0.0.2
amazing
之前好像是不可以的,可能跟我的npm和git的版本有關(guān)系 - git version 2.20.1.windows.1
- npm -v 6.14.5 node -v v12.18.1
npm version patch -m "Upgrade to %s for reasons"
加上評論更明確目的。
6. yarn add html2pdf.js 報錯
An unexpected error occurred: "https://raw.githubusercontent.com/eligrey/FileSaver.js/e865e3 7af9f9947ddcced76b549e27dc45c1cb2e/package.json: getaddrinfo ENOENT raw.githubusercontent.com raw. githubusercontent.com:443
其實這個錯誤并不是html2pdf.js 曝出來的,而是安裝jspdf報錯的。jspdf版本為1.5.3,降級安裝jspdf@1.4.1就沒問題。Cannot install jspdf 1.5.3。 html2pdf.js當前版本為0.9.2,到github看他的依賴,
package.json
"dependencies": {
"es6-promise": "^4.2.5",
"html2canvas": "^1.0.0-alpha.12",
"jspdf": "^1.4.1"
},
package.lock.json
"jspdf": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/jspdf/-/jspdf-1.4.1.tgz",
"integrity": "sha512-2vYVdrvrQUdKKPyWHw81t1jEYYAJ6uFJ/HtTcGbI4qXIQEdl18dLEuL2wTeSv2GzeQLSgUvEvwsXsszuHK+PTw==",
"requires": {
"canvg": "^1.0",
"cf-blob.js": "0.0.1",
"file-saver": "1.3.8",
"omggif": "1.0.7",
"stackblur": "^1.0.0"
}
},
yarn 依賴的版本對 ^ 字符范圍 做了說明
字符 ^ 表明不會修改版本號中的第一個非零數(shù)字,3.1.4 里的 3 或者 0.4.2 里的 4。 如果不考慮package.lock.json鎖定的版本,按照以上說法讀取package.json中的dependencies版本信息的話,yarn add html2pdf.js的時候安裝的就是jspdf的最新版本1.5.3.所以會報錯。但是用npm install html2pdf.js就不會報錯,安裝是上的jspdf的版本是1.5.3,這就很費解。
yarn.lock
html2pdf.js@^0.9.2:
version "0.9.2"
resolved "https://registry.yarnpkg.com/html2pdf.js/-/html2pdf.js-0.9.2.tgz#9f6d2b57fcfe48ee6deb42e1b3eb56fe4e3a4b22"
integrity sha512-lWV/dZEDfwX6tGQ2mrjyMCiEgXp9bLn74nxzGkuzWYeWMJ/0col2BJfVI7rflh2vkQd7PXEHTZ6AYe7m8OPrlQ==
dependencies:
es6-promise "^4.2.5"
html2canvas "^1.0.0-alpha.12"
jspdf "^1.4.1"
dependencies 字段記錄的是當前包的依賴,即當前包在 package.json 的 dependencies 字段中的所有依賴。
對于報錯的解決辦法就是先yarn add jspdf@1.4.1 然后再去yarn add html2pdf.js 。后面再安裝檢測到j(luò)spdf@1.4.1存在,符合依賴要求,這樣的話就不會重復(fù)安裝jspdf,合理的避開報錯問題。
7. yarn
yarn global add weinre 安裝全局依賴
yarn global ls 查看全局包
yarn global dir 查看全局包位置
8. weinre啟動報錯
$ weinre --boundHost 192.168.100.183
2020-10-21T02:29:35.849Z weinre: starting server at http://192.168.100.183:8080
(node:17320) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
C:\Users\bill\AppData\Local\Yarn\Data\global\node_modules\connect\lib\middleware\static.js:144
type = mime.lookup(path);
^
TypeError: mime.lookup is not a function
(Use `node --trace-uncaught ...` to show where the exception was thrown)
解決辦法:
找到connect\lib\middleware\static.js 更改21行為 mime = require('mime-types')
9. windows 上 npm publish 報錯
Error: EPERM: operation not permitted, rename
解決辦法:
關(guān)閉防火墻
I got the same problem just doing an npm install.
Run with antivirus disabled (if you use Windows Defender, turn off Real-Time protection and Cloud-based protection). That worked for me!
Error: EPERM: operation not permitted, rename
9. yarn 通過git url安裝依賴
- http方式
yarn add <git remote url>#<branch/commit/tag> installs a package from a remote git repository at specific git branch, git commit or git tag.
比如:
yarn add https://gitee.com/qihoo360/spriteJS.git\#v3.0.6
- ssh方式
需要在前面加上 git+ssh://
yarn add git+ssh://git@github.com:ccorcos/oy.git#0.10.1
一般通過git url的時候會緩存,yarn cache dir
找到緩存目錄,該目錄下有個.tmp目錄,存放所有通過git url 安裝的依賴緩存,且該緩存的代碼是master分支的,并不是我安裝tag的代碼,不是很理解