【英】Better NPM'ing, Tips and Tricks using NPM

2021-02-25 前端早讀課

前言

昨天全英文的文章能接受這種短篇的方式嗎?

正文從這開始~~

I discovered several tips working with NPM on a daily basis. Here are the top ones.

TL;DR: save-exact, npm ci, npm audit fix, npx, updtr, NVM_SYMLINK_CURRENT

I presented those tips to my coworkers, the slides are available online.

Reproducible builds

Problem: your local install can/will differ from another coworkers, even on the CI server!

Cause: Version range are problematic: 「rxjs」: 「^6.2.2」

Greenkeeper.io tells us that 15% of packages break the minor or patch updates:

Solution: Use —save-exact when installing a dependency

$ npm install --save-exact aDependency
# Shorter:
$ npm i -E aDependency

Better solution: Always exact, never use a range: npm config set save-exact true

$ npm config set save-exact true

Installing package

Problem: Using npm install will tries to resolves the dependency graph, possibly installing different versions (because of ranges declared in dependencies, not yours even if you used —save-exact) and then updating the package-lock.json even if you did not want to.

Solution: Use npm ci which only read the package-lock.json

↗ Speed (on CI and locally)

➕ Avoid dirty-ing the package-lock.json

$ npm ci

Global package

Problem: Polluting the global node_modules with global packages: nest-cli, create-react-apps (= hundreds of packages)

Solution: npx runs a package without installing it (but first, tries to find it locally in node_modules)

# Example with params given to cleaver
$ npx cleaver watch index.md

Security

Problem: Finding packages with security flaws

Solution: Use the builtin npm audit and npm audit fix

➕ Fails the build given integrated it in CI

Another solution is to use the builtin services of Github and Gitlab.

$ npm audit fix

Updating packages

Problem: Updating dependency and finding the one that breaks the code is tedious.

Solution 1 (best): updtr update one dependency, then run the tests, then repeat

$ npx updtr

Solution 2: npm-check show a pretty menu of all updates

$ npx npm-check -u

Current Node version in Tools

Problem: When configuring Node/Typescript, the node path is version-dependent

Solution: if you use NVM for managing installation of Node.js, NVM can automatically manage a symlink to the current version of node. NVM will link ~/.nvm/current to, for example, ~/.nvm/versions/node/v11.0.0 and recreate the link when changing of node version (automatically if you use NVM auto-use ZSH plugin).

# Put this in your .bashrc/.zshrc
$ export NVM_SYMLINK_CURRENT=true

(Bonus) Follow Github Releases

Problem: Be notified of releases

Solution 1: (Updated: 2018.12.02) Github now support watching releases of a repository: Documentation.

Solution 2: Gitpunch.com seems to solve the problem. It can follow all your Github stars and specific projects.

最後,文中提到的PPT:https://tomsquest.github.io/presentation-better-npm-ing/

關於本文
作者:@Thomas Queste
原文:http://www.tomsquest.com/blog/2018/10/better-npm-ing/

相關焦點

  • 14 sharp tips for patch clamping
    In no particular order, here are 14 tips and tricks to help you improve your technique so that you can become more efficient and productive at patching cells.1. Ready, Steady, Go!
  • 【英文歌曲】Better In Time——一切都會好起來的
    正如它低調地登上了Billboard榜hot100的第11名一樣,這首歌也被提名了全英音樂獎的英國最佳單曲。in time) (一切都會好起來)And even though I really love you 儘管我是真的愛你I'm gonna smile 我會微笑cause I deserve too ooh 因為我值得(It'll all get better in time) (一切都會好起來)Since there's no more you and me
  • 尤克裡裡譜:|五月天'[戀愛ing]
    而最初只準備收錄三首新作品的打算,由於考量到呈現一張有完整概念及創造迥然不同一般所謂「精選集」的全新聆聽體驗,五個人竟然一口氣完成了六首風格各異、各有不可或缺意涵的新作品收錄,《戀愛ing》就是這新創的六首之一。「Be動詞+動詞ing」是英文的現在進行式,五月天始終和音樂戀愛ing,所以也快樂ing, 「快樂到讓人想到海邊裸奔!」
  • The Top Ten Tips to Survive Spring Festival | 春節十大生存技巧
    在這個視頻中,我們要談談This video is 'Top 10 tips「春節十大生存技巧」on how to survive the Spring Festivaland not lose your mind'.
  • 刷牙英語:除了「小費」,tips還有什麼意思?
    Little Ma: Yes, but they work in hospitality, so they rely on tips. My friend was wondering if there is a polite way to ask customers for a tip?
  • 英文歌曲推薦:我會好好的《Better In Time》
    正如它低調地登上了Billboard榜hot100的第11名一樣,這首歌也被提名了全英音樂獎的英國最佳單曲。總之這首歌在全球獲得了眾多的榮譽和好評,是一首名副其實的經典熱門單曲。記得曾有網友對這首歌如此評價:「外國的歌都是如何走出失戀陰影,變得更好,而國內的都是讓人繼續傷心墮落。」這樣的評價充分說明了這首歌的治癒與暖心程度。到底有多好聽?還是快跟小編去感受下吧!
  • 英語語法:後接不定式和-ing式意義不同的情況
    、不定式表動作,動詞-ing式指一般情況、後接不定式或-ing式意義沒多大差別的情況,本期學習英語語法:後接不定式和-ing式意義不同的情況。‍‍‍‍‍動詞-ing形式是一種非謂語形式,其可以在句子中充當除謂語動詞以外的多種成分。
  • 聽歌學英語:《Better in Time》
    用我知道的一切)Imma be ok沒關係Thought i couldn't live without you想到我不能沒有你獨自生活It's gonna hurt when it heals too傷口癒合,與此同時也會痛Oh yeah哦,耶(It'll All get better
  • 【生活ing】這份全新澳洲替代版入籍測試,居然連Aussie們都答不上來!
    Which of these sports do Aussies know they are better at than prommies?澳洲人認為他們哪項運動比英國人玩得好?ing推廣雪梨ing廣告諮詢:ads@sydneying.com
  • 今晚@ALL:斯德哥爾摩綜合症 - Tzusing '一瞬千擊' Release Party
    Tzusing在紐約廠牌L.I.E.S.Tzusing從上張全長專輯中提煉出的精華濃縮形成的5首歌曲EP 『一瞬千擊』 為今年畫上了一個句號。Tzusing將固定音樂種類的枷鎖丟遠遠到了一邊,這讓他成為了techno世界中最令人激動的藝人之一。這番灑脫與不屑讓 『一瞬千擊』 變成了本年度聽起來最爽快的techno作品之一。
  • WELL PLANET | Eco-Friendly Tips For A Green Halloween
    Go for minimalist costumes using what you already have at home (my daughter went as a Panda last year, with a minimalist Panda felt mask, and a beige outfit she already owned).