title: Modify environment with tests while running Postman Newman in NodeJS
body: <p>I'm running Newman in a Node Script. The collection has environment variables such as <code>{{claimNum}}</code> that would increment with each post by the test.</p>
  
  <p><strong>Example</strong>:</p>
  
  <p>I have this <strong>collection</strong> request body</p>
  
  <pre><code> &lt;ClaimIdentification&gt;
        &lt;company&gt;0001&lt;/company&gt;
        &lt;office&gt;100&lt;/office&gt;
        &lt;claimNum&gt;{{claimNum}}&lt;/claimNum&gt;
   &lt;/ClaimIdentification&gt;
  </code></pre>
  
  <p>And in the <strong>global environment</strong> JSON:</p>
  
  <pre><code>  {
    "enabled": true,
    "key": "claimNum",
    "value": "15200",
    "type": "text"
  },
  </code></pre>
  
  <p>And the collection has this <strong>test</strong>:</p>
  
  <pre><code>pm.globals.set("claimNum", + pm.globals.get("claimNum") + 1);
  </code></pre>
  
  <p>But when ran in the script, the global.json file won't be modified and the "value" would stay the same. When this same parameters are run in the desktop app, it works.</p>
  
  <p>Is there a solutions for this, should this work?</p>
  
  <p><strong>UPDATE1:</strong></p>
  
  <p>This is the newman script:</p>
  
  <pre><code>  collection: require('${__dirname}/ThirdServiceInjured.json'),
    reporters: 'json',
    globals: require('${__dirname}/globals.json')
  
  }).on('done', function (err, summary) {
    if (err || summary.error) {
        console.error('&gt;&gt;&gt; ERROR - Collection run failed.');
    }
    else {
      console.log(success('Collection run completed:'));  
      console.log(summary.run.executions[0].response.text());
    }
  });
  </code></pre>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Using this script and still not writing over the enviroment:</p>
  
  <pre><code>const newman = require('newman'); // require newman in your project
  const fs = require('fs');
  const envName = '${__dirname}/environment_qbe600.json';
  const env = require('${__dirname}/environment_qbe600.json');
  
  newman.run({
    collection: require('${__dirname}/ThirdServiceInjured.json'),
    reporters: 'cli',
    environment: envName,
    iterationCount: 3
  
  }).on('done', function (err, summary) {
    if (err || summary.error) {
        console.error('&gt;&gt;&gt; ERROR - Collection run could failed.');
  }
  else {
    const newmanValue = summary.environment.values.members[0].value;
    env.values[0].value = newmanValue;
    console.log(summary.run.executions[0].response.text());
    fs.writeFile(envName, JSON.stringify(env, null, 2), function (err) {
      if (err) return
    })
  }
  });
  </code></pre>
  
  <p><strong>UPDATE 3:</strong></p>
  
  <p>This is the enviroment:</p>
  
  <pre><code>{
    "id": "ecabb925-829e-69f8-2348-f71dc76c0e87",
    "name": "Test",
    "values": [
      {
        "enabled": true,
        "key": "host",
        "value": "${___server}",
        "type": "text"
      },
      {
        "enabled": true,
        "key": "company",
        "value": "0001",
        "type": "text"
      },
      {
        "enabled": true,
        "key": "claimNbr",
        "value": "14600",
        "type": "text"
      },
      {
        "enabled": true,
        "key": "dni",
        "value": "150",
        "type": "text"
      },
      {
        "enabled": true,
        "key": "cost",
        "value": "107000",
        "type": "text"
      },
      {
        "enabled": true,
        "key": "testNum",
        "value": "157",
        "type": "text"
      }
    ],
    "timestamp": 1515789551332,
    "_postman_variable_scope": "environment",
    "_postman_exported_at": "2018-01-12T20:39:14.795Z",
    "_postman_exported_using": "Postman/5.5.0"
  </code></pre>
  
  <p>And the test section from the collection:</p>
  
  <pre><code>"event": [
      {
          "listen": "test",
          "script": {
              "type": "text/javascript",
              "exec": [
                  "pm.environment.set(\"claimNbr\", +pm.environment.get(\"claimNbr\") + 1);",
                  "pm.environment.set(\"testNum\", +pm.environment.get(\"testNum\") + 1);",
                  "pm.environment.set(\"dni\", +pm.environment.get(\"dni\") + 1);",
                  "pm.environment.set(\"cost\", +pm.environment.get(\"cost\") + 1000);"
              ]
          }
      }
  ],
  </code></pre>