如何在本地測試NPM軟件包

如果要開發自己的npm軟件包,則首先必須在本地對其進行測試。

對於一個我想模塊化的項目,我有這個需求。

我有一個叫我的包裹,例如flaviocopes-common-database

我以為前提flaviocopes-給它一個唯一的命名空間。

在包裝內,我添加了一個package.json文件中帶有模塊名稱的文件name屬性和一些依賴項:

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

然後我跑了

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.

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:

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

Download my free Node.js Handbook


More node tutorials: