npm本地包安装_如何在本地测试NPM软件包

npm本地包安装

If you want to develop your own npm package, you first have to test it locally.

如果要开发自己的npm软件包,则首先必须在本地对其进行测试。

I had this need with a project that I wanted to modularize.

我对一个我想模块化的项目有这种需求。

I had a package I called, as an example, flaviocopes-common-database.

我有一个名为flaviocopes-common-database的软件包。

I prepended flaviocopes- to give it a unique namespace.

我在flaviocopes-添加了一个唯一的名称空间。

Inside the package I added a package.json file with the module name in the name property and a few dependencies:

在包内,我添加了一个package.json文件,其模块名在name属性中,并具有一些依赖性:

{
  "name": "flaviocopes-common-database",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "pg": "^8.0.2",
    "sequelize": "^5.21.6"
  }
}

Then I ran

然后我跑了

npm link

This created a symbolic link in the /usr/local/lib/node_modules/ folder, that contains the global npm packages in the system, the ones installed using npm -g, to be clear.

这在/usr/local/lib/node_modules/文件夹中创建了一个符号链接,其中包含系统中的全局npm软件包,这些软件包使用npm -g了安装。

I had

我有

/usr/local/lib/node_modules/flaviocopes-common-database

Pointing to the local file I had in

指向我所在的本地文件

/Users/flavio/dev/code/flaviocopes-common-database

Now in another project I wanted to use this module, so I ran

现在在另一个项目中,我想使用此模块,所以我运行了

npm link flaviocopes-common-database

and I was able to import it in the Node.js code using the usual require() syntax:

并且我能够使用通常的require()语法将其导入到Node.js代码中:

const database = require('flaviocopes-common-database')

翻译自: https://flaviocopes.com/npm-local-package/

npm本地包安装

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值