npm 更新 npm

by Adir Amsalem

由Adir Amsalem

您可以使用8 npm技巧来打动同事 (8 npm Tricks You Can Use to Impress Your Colleagues)

You watch a colleague coding, there’s a shorthand or trick being applied, somehow you’re not familiar with it and your mind blows away. Happens to all of us all the time.

您观看同事的编码,正在应用速记或技巧,以某种方式您不熟悉它,并且头脑飞快。 一直发生在我们所有人身上。

In this short post we will unveil some very useful npm tricks. There are many more than what we can cover here, so I chose to focus on those that are most relevant and useful for our day-to-day workflow as developers.

在这篇简短的文章中,我们将揭示一些非常有用的npm技巧。 这里除了我们可以涵盖的内容之外,还有很多其他内容,因此我选择专注于与开发人员日常工作流程最相关和最有用的内容。

开始之前的基本速记 (Basic shorthands before we’re getting started)

To get everyone aligned, especially the newcomers among us, let’s have a quick overview of the basic shorthands and make sure nobody miss anything trivial.

为了使所有人(尤其是我们当中的新手)保持一致,让我们快速浏览一下基本速记,并确保没有人错过任何琐碎的事情。

安装软件包: (Installing a package:)

Regular: npm install pkg, Shorthand: npm i pkg.

常规: npm install pkg ,简写: npm i pkg

全局安装软件包: (Installing a package globally:)

Regular: npm i --global pkg, Shorthand: npm i -g pkg.

常规: npm i --global pkg ,简写: npm i -g pkg

安装软件包并将其另存为依赖项: (Installing a package and save it as a dependency:)

Regular: npm i --save pkg, Shorthand: npm i -S pkg.

常规: npm i --save pkg ,简写: npm i -S pkg

安装软件包并将其另存为devDependency: (Installing a package and save it as a devDependency:)

Regular: npm i --save-dev pkg, Shorthand: npm i -D pkg.

常规: npm i --save-dev pkg ,简写: npm i -D pkg

For additional shorthands read npm’s own shorthand list.

有关其他速记,请阅读npm自己的速记列表 。

Let’s begin with the interesting stuff now.

让我们从有趣的东西开始。

1.初始化新程序包 (1. Initializing a new package)

We all know npm init, it’s the first thing we do when creating a new package.

我们都知道npm init ,这是我们创建新程序包时要做的第一件事。

But, all those questions are quite annoying and we gonna modify it anyway, so why not just avoid it?

但是,所有这些问题都很烦人,无论如何我们都会对其进行修改,那么为什么不避免它呢?

npm init -y and npm init -f to the rescue!

npm init -ynpm init -f进行救援!

2.运行测试 (2. Running tests)

Another command we all use is npm test. Most of us use it every day, several times a day.

我们都使用的另一个命令是npm test 。 我们大多数人每天都使用它,一天几次。

What if I told you that you can do the same with ~40% less characters? We use it so much, so it’s a nice win.

如果我告诉过您,您可以减少40%的字符,怎么办? 我们使用了很多,所以这是一个不错的胜利。

Fortunately, there’s npm t, which does exactly that!

幸运的是,这就是npm t

3.列出可用的脚本 (3. List available scripts)

We get to a new project and we wonder how to get started. We usually ask ourselves things like: how do we run it? which scripts are available?

我们进入一个新项目,我们想知道如何开始。 我们通常会问自己这样的事情:我们如何运行它? 哪些脚本可用?

One way to discover is to open the package.json file and check the scripts section.

发现方法之一是打开package.json文件并检查scripts部分。

We can do better of course, so we simply run npm run and get a list of all the available scripts.

当然,我们可以做得更好,因此我们只需运行npm run并获取所有可用脚本的列表。

Additional option is to install ntl (npm i -g ntl), and then run ntl in the project’s folder. It also allows to run the scripts, which makes it very convenient.

附加选项是安装ntl ( npm i -g ntl ),然后在项目的文件夹中运行ntl 。 它还允许运行脚本,这非常方便。

4.列出已安装的软件包 (4. List installed packages)

Similar to available scripts, sometimes we ask ourselves which dependencies we have in our project.

与可用脚本类似,有时我们会问自己在项目中拥有哪些依赖项。

We can once again open the package.json file and check, but we already know we can do better.

我们可以再次打开package.json文件并进行检查,但是我们已经知道我们可以做得更好。

Meet npm ls --depth 0.

npm ls --depth 0

To list the globally-installed packages, we can run the same with -g flag, npm ls -g --depth 0.

要列出全局安装的软件包,我们可以使用-g标志npm ls -g --depth 0来运行相同的软件包。

5.运行本地安装的可执行文件 (5. Running locally-installed executables)

We installed a package in our project, it comes with an executable, but it only works when we run it via an npm script. Did you wonder why, or how to overcome it?

我们在项目中安装了一个程序包,它带有一个可执行文件,但是仅当我们通过npm脚本运行它时,它才有效。 您是否想知道为什么,或者如何克服呢?

First, let’s understand why — when we execute commands in our terminal, what actually happens is that it looks for an executable with the same name in all the paths that are listed in our PATH environment variable. That’s how they’re magically available from anywhere. Locally-installed packages register their executables locally, so they aren’t listed in our PATH and won’t be found.

首先,让我们了解原因-当在终端中执行命令时,实际上发生的是,它在PATH环境变量中列出的所有路径中寻找具有相同名称的可执行文件。 这样便可以从任何地方神奇地使用它们。 本地安装的软件包在本地注册其可执行文件,因此它们未在我们的PATH列出,也不会找到。

How does it works when we run those executables via an npm script you ask? Good question! It’s because when running this way, npm does a little trick and adds an additional folder to our PATH, <project-directory>/node_modules/.bin.

当我们通过您要求的npm脚本运行那些可执行文件时,它如何工作? 好问题! 这是因为以这种方式运行时,npm会做一些技巧,并向我们的PATH <project-directory>/node_module s / .bin中添加一个额外的文件夹。

You can see it by running npm run env | grep "$PATH". You can also run just npm run env to see all the available environment variables, npm adds some more interesting stuff.

您可以通过运行npm run env | grep "$PATH"来查看它npm run env | grep "$PATH" npm run env | grep "$PATH" 。 您也可以只npm run env来查看所有可用的环境变量,npm添加了一些更有趣的东西。

node_modules/.bin, if you wondered, is exactly where locally-installed packages place their executables.

如果想知道, node_modules/.bin正是本地安装的软件包放置可执行文件的位置。

Let’s run ./node_modules/.bin/mocha in our project’s directory to see it in action.

让我们在项目目录中运行./node_modules/.bin/mocha ,看看它的实际效果。

Simple, right? Just run ./node_modules/.bin/<command> whenever you want to run a locally-installed executable.

简单吧? 只要您想运行本地安装的可执行文件,只需运行./node_modules/.bin/<comma nd>。

6.在互联网上找到您的包裹 (6. Find your package on the internet)

You might came across the repository entry in the package.json file and wondered: “What is it good for?”.

您可能会在package.json文件中遇到repository条目,并想知道:“这有什么用?”。

To answer it, simply run npm repo and watch it open in your browser .

要回答该问题,只需运行npm repo并在浏览器中查看它是否已打开。

Same applies, by the way, for the npm home command and the homepage entry.

顺便说一句,同样适用于npm home命令和homepage条目。

If you want to open your package page on npmjs.com, there’s a nice shorthand for that as well, npm docs.

如果您想在npmjs.com上打开您的软件包页面, npm docs也是一个很好的简写。

7.在其他脚本之前和之后运行脚本 (7. Run scripts before and after other scripts)

You’re probably familiar with scripts such as pretest, which allows you to define code that would run before the test script.

您可能已经熟悉诸如pretest脚本,该脚本可让您定义将在test脚本之前运行的代码。

What you might be surprised to find out, is that you can have pre and post scripts for every script, including your own custom scripts!

您可能会惊讶地发现,您可以为每个脚本添加前后脚本,包括您自己的自定义脚本!

It’s very useful for projects in which you use npm as your build tool and have many scripts you need to orchestrate.

这对于使用npm作为构建工具并需要编排许多脚本的项目非常有用。

8.碰撞包的版本 (8. Bumping package’s version)

You have a package, you use semver for versioning, and you need to bump the version before a new release.

您有一个程序包,使用semver进行版本控制,并且需要在发布新版本之前更改版本。

One way to do this is to open the package.json file and change the version manually, but we’re not here for that.

一种实现方法是打开package.json文件并手动更改版本,但是我们不准备这样做。

An easier way is to run npm version with major, minor or patch.

一种更简单的方法是使用majorminorpatch运行npm version

That’s all for now.

目前为止就这样了。

I hope you learned something new and found at least one of those tricks useful for your day-to-day workflow, and ideally you also know npm better now and have some new ideas for how you can utilize it better in your job.

我希望您学到了一些新知识,并至少找到了对您的日常工作流程有用的一些技巧,并且理想情况下,您现在也对npm有所了解,并对如何更好地利用它有一些新的想法。

Impressing your colleagues is great, but constantly learning new things and being more professional is even better!

给您的同事留下深刻的印象,但是不断学习新事物和变得更加专业会更好!

If you know additional useful tricks, please share them in the comments!

如果您知道其他有用的技巧,请在评论中分享!

翻译自: https://www.freecodecamp.org/news/8-npm-tricks-you-can-use-to-impress-your-colleagues-dbdae1ef5f9e/

npm 更新 npm

npm 更新 npm_您可以使用8 npm技巧来打动同事相关推荐

  1. npm 更新 npm_这些NPM技巧将使您成为专业人士

    npm 更新 npm By Carl-Johan Kihl 卡尔·约翰·基尔(Carl-Johan Kihl) After using NPM for almost 8 years, I've lea ...

  2. npm 端口设置成80_13 个 NPM 快速开发技巧

    摘要: 玩转npm. 作者:前端小智 原文:13 个 npm 快速开发技巧 Fundebug经授权转载,版权归原作者所有. 为了保证的可读性,本文采用意译而非直译. 每天,数以百万计的开发人员使用 n ...

  3. npm更新模块并同步到package.json中

    使用原始npm 1.查看需要更新的版本 npm outdated 该命令会列出所有需要更新的项目 2.修改package.json中需要更新的包对应的版本号 npm update 由于npm upda ...

  4. npm更新和nodejs更新

    npm更新和nodejs更新 更新你已经安装的NPM库,这个很简单,只需要运行. npm update -g 更新Nodejs自身.一直依赖我都是下载最新版的源码,然后make install,及其繁 ...

  5. npm更新包(全局单个,项目单个,全局所有,项目生产环境,项目开发环境)

    npm-check检查npm依赖包是否有更新,错误以及不在使用的. 安装npm-check npm install -g npm-check npm全局更新包 npm-check -u -g npm更 ...

  6. 使用npm更新所有依赖包到最新版本

    本篇文章参考文章 - npm包之npm-check-updates 文章目录 npm-check-updates 背景交代 npm-check-updates   一键升级所有依赖的插件为 npm-c ...

  7. npm 更新升级(最新版本 | 指定版本)

    查看 npm 当前版本 $ npm -v 更新到最新版本 $ npm install -g npm 更新到指定版本 $ npm -g install npm@6.8.0 清理 npm 缓存数据 $ n ...

  8. npm更新命令更新最新版本

    npm更新版本 1.打开cmd窗口,查看当前版本 npm -v 2.更新指令 npm install -g npm 3.或者更新指定版本 npm -g install npm@8.3.1

  9. 如何使用npm更新项目依赖包

    NPM(Node Package Manager,结点包管理)是一款用于管理 Node.js 依赖包的工具.通过 NPM,开发者可以快速地安装.卸载.更新等项目所需的依赖包,从而有效提高开发效率.节省 ...

最新文章

  1. 40个出色的Wordpress cms插件
  2. 大数据 清华 覃征_调剂到清华读研?不想去!清华大学大数据调剂生放弃录取!...
  3. 解决首次访问jenkins,输入默认密码之后,一直卡住问题
  4. 十种机制保护三大网络基础协议(BGP、NTP和FTP)—Vecloud微云
  5. C#学习之Reflection
  6. 想学习Android开发
  7. socket聊天室笔记
  8. 数据库表命名 单数复数_数据是还是数据是? “数据”一词是单数还是复数?
  9. 7.10.8107.79 小心80180048
  10. Java 2实用教程(第5版)实验指导与习题解答 第3章-上机实践-分支与循环语句
  11. 基于MATLAB语音分析处理系统
  12. JavaScript 每日一题 #9
  13. 团队管理:新业务团队如何结合绩效来度量开发目标
  14. 资深运维给你讲:一个小白的运维成长故事
  15. 考研线性代数手写笔记3 向量
  16. 【Vue】Vue中的data数据包含html标签元素的解决方法
  17. 公众号互推还是要的,要不怎么让更多人看到可爱的我呢?
  18. 基于表格的CRC校验码实现
  19. (48)STM32——图片显示实验
  20. C02-程序设计基础提高班(C++)第8周上机任务-结构体

热门文章

  1. ISCC2021 Web WP
  2. graphpad画生存曲线怎么样去掉删失点_手把手教你用GraphPad Prism绘制生存曲线
  3. 对策论基础---引言
  4. 两年多的社招经验分享,我的跳槽经验总结(含阿里滴滴美团快手头条)
  5. 蓝桥杯- 算法训练-Beaver's Calculator
  6. 单调有界定理适用于函数吗_实数的完备性定理
  7. linux c++ 线程支持 多核应用,linux C++多线程服务端开发
  8. 华为机考,华为笔试,软件类,2020年8月19日题目,超详细解答。
  9. ExtJS 前端 日期数据格式转化
  10. 家里的垃圾移动光猫和tp路由器设置