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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值