Using a package from a second package

I have created two packages. Let’s call them ‘a’ and ‘b’ for my team ‘t’.

In package b I would like to call myFunction from package a.

I have tried the following:

// package b
const a = pm.require('@t/a');

function mySecondFunction() {
   a.myfunction();
}

module.exports = { mySecondFunction };

When I try to use package b in a request script I get the following error:

Error in package '@t/b': Cannot find module '@t/a'

Is this supported? If yes, how should I change my code in package b so I can call functions from package a?

Hey @tibiextera :wave:t2:

Referencing packages inside other packages is not currently supported.

Could you explain more about your use case and the need to use a script in this way? It will help us to better understand your requirement.

I am creating tests for the current REST API that I am working on. Some tests require that I invoke the REST API with other calls than the main request to set up the data that the main request will use for the test.

I created a package that turns the pm.sendRequest function into an async function that returns a Promise.

I was planning to use this in a package that is specific for this current API, while still having it available for other APIs that I might build in the future.

That led to requiring a package from a second package, which, after some more research, seems that it is not possible currently.

I am doing the same thing, and was able to get around this issue by requiring package a in the request script where I called package b, as well as in package b itself.

Same here. The clumsy workaround is to require the same dependencies in the package and in the calling script, but this is clearly a misfeature. Require should work correctly in packages. The need for this is obvious. It is the same as why we need packages in the first place: sharing and reusing code.

In my case I wrote some general-purpose utilities, then tried to use them in more specialized packages when I ran into this issue.

We appreciate the feedback @dmitry-epstein-rjf. :pray:t2:

The Package Library is still a relatively new feature in Postman which gaining traction amongst the user base.

It’s not a finished feature and we’re always gathering feedback in order to improve the overall functionality for you all.

This feature request is on the roadmap, as well as a bunch of other items that have been requested.

There is no timeline for these features just yet but more information will be shared once we have some new updates for you all.

Appreciate you using the feature and please do keep the feedback coming, it’s the best way that we can improve Postman for everyone.

1 Like

Thank you for keeping this request in mind. The packages feature could be a really useful feature when this and another couple of issues will be resolved.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.