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')
npm本地包安装