# Environment variable scope when set in a function defined in a collection or folder prerequest script

**URL:** <https://community.postman.com/t/environment-variable-scope-when-set-in-a-function-defined-in-a-collection-or-folder-prerequest-script/1824>\
**Category:** Help Hub\
**Tags:** tests, collections, folders\
**Created:** [June 7, 2018, 12:48pm UTC](https://community.postman.com/t/environment-variable-scope-when-set-in-a-function-defined-in-a-collection-or-folder-prerequest-script/1824 "2018-06-07T12:48:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JessePelton](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@JessePelton](https://community.postman.com/u/JessePelton)\
**Post date:** [June 7, 2018, 12:48pm UTC](https://community.postman.com/t/environment-variable-scope-when-set-in-a-function-defined-in-a-collection-or-folder-prerequest-script/1824/1 "2018-06-07T12:48:32Z")

</div>

I find it useful to define functions in collection or folder pre-request scripts for operations that are performed in multiple requests in the collection or folder. When I set an environment variable in such a script, the value persists for the duration of the function call, but when the function returns, the value is no longer set in the environment. It’s as if these scripts create an additional variable scope for a local environment.

Here’s an export of a simple test collection that demonstrates the behavior.

```auto
`{`
` "info": {`
` "_postman_id": "aedb7344-f32d-4395-b42a-b5153359682a",`
` "name": "Environment scope test",`
` "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"`
` },`
` "item": [`
` {`
` "name": "Folder",`
` "description": "",`
` "item": [`
` {`
` "name": "Environment scope test",`
` "event": [`
` {`
` "listen": "test",`
` "script": {`
` "id": "080e8815-6322-473c-8fe8-4d6a1e4cdfef",`
` "type": "text/javascript",`
` "exec": [`
` "requestFunction = function() {",`
` " const value = 'requestFunction';",`
` " pm.environment.set('testVariable', value);",`
` " pm.test('in function: testVariable environment variable is set to \"' + value + '\"', function() {",`
` " pm.expect(pm.environment.get(\"testVariable\")).to.equal(value);",`
` " });",`
` "};",`
` "",`
` "pm.environment.clear('testVariable');",`
` "collectionFunction();",`
` "pm.test('after collectionFunction: testVariable environment variable is set to \"collectionFunction\"', function() {",`
` " pm.expect(pm.environment.get(\"testVariable\")).to.equal('collectionFunction');",`
` "});",`
` "",`
` "folderFunction();",`
` "pm.test('after folderFunction: testVariable environment variable is set to \"folderFunction\"', function() {",`
` " pm.expect(pm.environment.get(\"testVariable\")).to.equal('folderFunction');",`
` "});",`
` "",`
` "requestFunction();",`
` "pm.test('after requestFunction: testVariable environment variable is set to \"requestFunction\"', function() {",`
` " pm.expect(pm.environment.get(\"testVariable\")).to.equal('requestFunction');",`
` "});"`
` ]`
` }`
` }`
` ],`
` "request": {`
` "method": "GET",`
` "header": [],`
` "body": {},`
` "url": {`
` "raw": "https://www.getpostman.com/",`
` "protocol": "https",`
` "host": [`
` "www",`
` "getpostman",`
` "com"`
` ],`
` "path": [`
` ""`
` ]`
` }`
` },`
` "response": []`
` }`
` ],`
` "event": [`
` {`
` "listen": "prerequest",`
` "script": {`
` "id": "df03439f-ddcb-4e7a-9b55-dcf0e821ebde",`
` "type": "text/javascript",`
` "exec": [`
` "folderFunction = function () {",`
` " const value = 'folderFunction';",`
` " pm.environment.set('testVariable', value);",`
` " pm.test('in folderFunction: testVariable environment variable is set to \"' + value + '\"', function() {",`
` " pm.expect(pm.environment.get(\"testVariable\")).to.equal(value);",`
` " });",`
` "};"`
` ]`
` }`
` },`
` {`
` "listen": "test",`
` "script": {`
` "id": "0da73ed7-eac4-4ce6-9e21-6dec17b94ec7",`
` "type": "text/javascript",`
` "exec": [`
` ""`
` ]`
` }`
` }`
` ]`
` }`
` ],`
` "event": [`
` {`
` "listen": "prerequest",`
` "script": {`
` "id": "50474e9f-bfe8-4a54-8497-1922d151d304",`
` "type": "text/javascript",`
` "exec": [`
` "collectionFunction = function() {",`
` " const value = 'collectionFunction';",`
` " pm.environment.set('testVariable', value);",`
` " pm.test('in collectionFunction: testVariable environment variable is set to \"' + value + '\"', function() {",`
` " pm.expect(pm.environment.get(\"testVariable\")).to.equal(value);",`
` " });",`
` "};"`
` ]`
` }`
` },`
` {`
` "listen": "test",`
` "script": {`
` "id": "64e501a7-b109-4431-a5c7-c94bf17b04de",`
` "type": "text/javascript",`
` "exec": [`
` ""`
` ]`
` }`
` }`
` ]`
`}`

```

Is this behaviour by design? I haven’t found any mention of it in the discussion of variable scopes.

Possibly related: when I want to define a function in a collection or folder pre-request script for use in a request test script, I have found it necessary to use the following pattern.  
`funcname = function(params) = { body }`  
If I define the function as a function rather than as a variable, the function is not recognized by the request test script.  
`function funcname(params) { body }`

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 5, 2018, 9:12pm UTC](https://community.postman.com/t/environment-variable-scope-when-set-in-a-function-defined-in-a-collection-or-folder-prerequest-script/1824/2 "2018-07-05T21:12:42Z")

</div>

@JessePelton I can see that you’ve used `pm.environment.clear`, which wipes out the entire environment. If you’re looking to remove a specific environment variable, use `pm.environment.unset` instead. Passing a variable name to `pm.environment.clear` has no special effect, but will still wipe out the entire environment.

Additionally, the execution scope is **not** shared across different scripts, so you will have to re-declare any functions that you’d like to use. We do understand that there is room for improvement here 😄

The sandbox API is documented here: [http://getpostman.com/docs/v6/postman/scripts/postman\_sandbox\_api\_reference](http://getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference)

---

<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:** [April 9, 2025, 2:30pm UTC](https://community.postman.com/t/environment-variable-scope-when-set-in-a-function-defined-in-a-collection-or-folder-prerequest-script/1824/3 "2025-04-09T14:30:18Z")

</div>

Closed on April 9, 2025, 2:30pm UTC


