Newman does not recognize "substring"

When running the collection via Newman (please let me know if there is another way), I get the following screen:

The errors seems to say that the variable is undefined, however, the requests run perfectly when I send them in postman.

Here is the “Tests” script:
postman.setEnvironmentVariable(“data”, pm.response.headers.get(“Location”));
var internal = pm.environment.get(“data”).substring(109);
postman.setEnvironmentVariable(“packlineid”, internal);

Additional note: I am exporting the collection as JSON.

How can I resolve this issue?

Are you exporting the environment.json as well and using that in the Newman command line?

No… I guess I need to find out how to do that. Any reference I can use?

OK… Thanks for the reference. I went ant Exported the environment, and then tried running the collection, however, I am now getting a not authorized error:

Please take a look at the command I used to run it, in the terminal… Am I missing something?

Here is the script in the Tests tab:

How come I get the 401 error if It works fine in postman?

OK… I have now gone and manually entered the values of the environment variables (they exported blanks) in the Environment JSON file, and successfully re-ran the collection with Newman. Thank you for the help.
There is only one question left now: Why did the environment export not include the environment variables values?

Its because of “initial” vs “current” values.

Initial values can be exported and shared with your team.

However its the current values that are used when sending a request and these are not exported (or synced to the Postman cloud).

If the current value is left empty, then it will automatically use the initial value (if its set).

A lot of the times, the environment variables are used for information that should not be stored in your code repository (or the Postman cloud).

Therefore you store these values elsewhere (for example, we use Azure Key Vault). Retrieve them as apart of your pipeline and then send them as individual values through the Newman command line.

If its not confidential information (like environment host names), then you can store the info in the initial value.

OK, mdjones… Thank you for your help with this issue.