# \[Postman v11\] Run a global function from a child folder

**URL:** <https://community.postman.com/t/postman-v11-run-a-global-function-from-a-child-folder/62281>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [June 17, 2024, 6:03am UTC](https://community.postman.com/t/postman-v11-run-a-global-function-from-a-child-folder/62281 "2024-06-17T06:03:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![monica.m](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@monica.m](https://community.postman.com/u/monica.m)\
**Post date:** [June 17, 2024, 6:03am UTC](https://community.postman.com/t/postman-v11-run-a-global-function-from-a-child-folder/62281/1 "2024-06-17T06:03:04Z")

</div>

How can I run a global function in a separate folder than the one where it is declared?

Ex: In the pre-request script of the collection I set the following:  
pm.globals.set(“collectionGlobalFunction”, colGlobalFunc = () =\> 3 + 5);

If I call this variable in the same pre-request script of the collection , like this, it works:  
console.log("This is global function called from collection pre-request : ");  
console.log(pm.globals.get(“collectionGlobalFunction”));

But if I’m using the console logs into a pre-request of a **child** folder (in the collection), it doesn’t work:

console.log("This is global function called from step with console output: ");  
console.log(pm.globals.get(“collectionGlobalFunction”));

It gives null.  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/d/9d88133cedc75d728dc6f8a4242154f58fbf8135.png)

Note: Before upgrading to Postman 11 it worked perfectly.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 17, 2024, 12:15pm UTC](https://community.postman.com/t/postman-v11-run-a-global-function-from-a-child-folder/62281/2 "2024-06-17T12:15:45Z")

</div>

Probably one for someone from Postman to answer.

I thought that code only executed from the current request, the current folder, or at the collection level.

If you have folders within folders, I didn’t think it would execute the code in the pre-requests for all of the folders in the structure. Only the current folder.

If it used to work, then fair play, hence I suspect someone from Postman will be able to advise further.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 17, 2024, 2:49pm UTC](https://community.postman.com/t/postman-v11-run-a-global-function-from-a-child-folder/62281/3 "2024-06-17T14:49:08Z")

</div>

Hey @monica.m 👋

Having no prior background into your context and only seeing a small sample of where you have used it and not seeing the full Collection to know what `colGlobalFunc` is doing - I have no comment about this working or not working prior to V11.

I would suggest looking at using the _Package Library_ feature in V11 for scripts that you want to use at a Global level.

> **[Reuse scripts in Postman | Postman Learning Center](https://learning.postman.com/docs/tests-and-scripts/write-scripts/package-library/)**
>
> Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.

All global functions that had been create prior to V11 were done as a workaround for Postman not having this capability in place.

---

<div class="post-metadata">

**Author:** ![ddegey](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ddegey/32/27840_2.png) [@ddegey](https://community.postman.com/u/ddegey)\
**Post date:** [July 1, 2024, 8:54am UTC](https://community.postman.com/t/postman-v11-run-a-global-function-from-a-child-folder/62281/4 "2024-07-01T08:54:45Z")

</div>

Hello @monica.m,

All variables declare in a pre-script are global in the sub-hierarchy as long as you don’t use var/const/let in the declaration.  
This works with postman v10 & v11.

For function, I use something like this in the top collection.

```auto
utils = {
    version: '1.0.2',
    base64url : function(source) {
        const crypto = require('crypto-js');
        // Encode in classical base64
        encodedSource = crypto.enc.Base64.stringify(source)
        
        // Remove padding equal characters
        encodedSource = encodedSource.replace(/=+$/, '')
        
        // Replace characters according to base64url specifications
        encodedSource = encodedSource.replace(/\+/g, '-')
        encodedSource = encodedSource.replace(/\//g, '_')
        
        return encodedSource
    },
    upperCaseFirst : function(str) {
        return str.charAt(0).toUpperCase() + str.slice(1);
    },
    getCurrentYear : function() {
        const now = new Date();
        return now.getFullYear();
    },
    getNextYear : function() {
        return utils.getCurrentYear() + 1;
    }
};

```

Next, in subfolders, I use “utils.getNextYear()”.

I always use a “version” property, because I don’t use the new v11 “package” in Postman.  
(At the moment, it can’t be used with Newman)

Hope this helps you.

Best

Didier

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [July 31, 2024, 8:55am UTC](https://community.postman.com/t/postman-v11-run-a-global-function-from-a-child-folder/62281/5 "2024-07-31T08:55:01Z")

</div>

Closed on July 31, 2024, 8:55am UTC

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