npm软件安装包_如何使用或执行使用npm安装的软件包

npm软件安装包

When you install using npm a package into your node_modules folder, or also globally, how do you use it in your Node code?

当您使用npm将软件包安装到node_modules文件夹中或全局安装时,如何在Node代码中使用它?

Say you install lodash, the popular JavaScript utility library, using

假设您使用以下命令安装了流行JavaScript实用程序库lodash

npm install lodash

This is going to install the package in the local node_modules folder.

这将把软件包安装在本地的node_modules文件夹中。

To use it in your code, you just need to import it into your program using require:

要在代码中使用它,您只需要使用require将其导入到程序中:

const _ = require('lodash')

What if your package is an executable?

如果您的软件包是可执行文件,该怎么办?

In this case, it will put the executable file under the node_modules/.bin/ folder.

在这种情况下,它将把可执行文件放在node_modules/.bin/文件夹下。

One easy way to demonstrate this is cowsay.

一种证明这一点的简单方法是考威

The cowsay package provides a command line program that can be executed to make a cow say something (and other animals as well 🦊).

Cowsay软件包提供了一个命令行程序,可以执行该程序以使母牛说出某些东西(以及其他动物也可以说话)。

When you install the package using npm install cowsay, it will install itself and a few dependencies in the node_modules folder:

当您使用npm install cowsay安装软件包时,它将安装自身以及node_modules文件夹中的一些依赖项:

The node_modules folder content

There is a hidden .bin folder, which contains symbolic links to the cowsay binaries:

有一个隐藏的.bin文件夹,其中包含指向Cowsay二进制文件的符号链接:

The binary files

How do you execute those?

你如何执行这些?

You can of course type ./node_modules/.bin/cowsay to run it, and it works, but npx, included in the recent versions of npm (since 5.2), is a much better option. You just run:

你当然可以键入./node_modules/.bin/cowsay来运行它,和它的作品,但NPX ,包括在最近的NPM的版本(因为5.2),是一个更好的选择。 您只需运行:

npx cowsay

and npx will find the package location.

然后npx将找到程序包的位置。

Cow says something

翻译自: https://flaviocopes.com/how-to-use-npm-package/

npm软件安装包

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值