简体   繁体   English

分配字符串后,process.env.VARIABLE变为未定义

[英]process.env.VARIABLE going to undefined after assigning string

I running tests with Mocha and Node in Windows, and the env variables are showing an weird behavior. 我在Windows中使用Mocha和Node运行测试,并且env变量显示出奇怪的行为。 Look the code below 看下面的代码

var stringMock = JSON.stringify(mock);
process.env.MOCKS = stringMock;
if(stringMock !== process.env.MOCKS) {
    console.log('typeof stringMock: ', typeof stringMock);
    console.log('typeof process.env.MOCKS: ', typeof process.env.MOCKS);
}

It's already very weird that the if statement evaluates to true , but the result of the log is: if语句的值为true已经很奇怪了,但是日志的结果是:

    typeof stringMock: string
    typeof process.env.MOCKS: undefined

How can this be even possible? 怎么可能呢? And if a ran this code in Codeship Linux, this doesn't happen, so probably can be a Node bug for Windows. 而且,如果在Codeship Linux中运行了此代码,则不会发生这种情况,因此可能是Windows的Node bug。

I suspect that Node has some kind of limitation about the size of the string for env variables, but I couldn't find anything useful about it. 我怀疑Node对env变量的字符串大小有某种限制,但是我找不到关于它的任何有用信息。

The real questions are: 真正的问题是:

  1. What is the value of mock ? mock价值是什么?
  2. What is the value of stringMock ? stringMockstringMock
  3. What is the value of process.env.MOCKS ? process.env.MOCKS是什么?

Having only the types is just part of the data. 仅具有类型只是数据的一部分。

I can imagine only one value for mock that would make stringMock !== process.env.MOCKS evaluate to true, and that is undefined . 我可以想象只有一个mock值会使stringMock !== process.env.MOCKS评估为true,并且该值是undefined That's because JSON.stringify() will return undefined instead of a string, and then thet undefined will get stringified when saved in the environment variables. 这是因为JSON.stringify()将返回undefined而不是字符串,然后将undefined保存在环境变量中时将被字符串化。

But strangely enough, the output for this program: 但奇怪的是,该程序的输出:

var mock = undefined;

var stringMock = JSON.stringify(mock);
process.env.MOCKS = stringMock;
if(stringMock !== process.env.MOCKS) {
    console.log('typeof stringMock: ', typeof stringMock);
    console.log('typeof process.env.MOCKS: ', typeof process.env.MOCKS);
}

is quite the opposite of what you're getting: 与您得到的相反:

typeof stringMock:  undefined
typeof process.env.MOCKS:  string

That's because the first one in the undefined value and the second one is a string "undefined" . 这是因为undefined值中的第一个是字符串"undefined" But here you have the opposite of that, which is quite strange and quite frankly I was unable to reproduce your result for any value that I have tried, like NaN , Infinity , 0 , '' , etc. Everything else serialized to JSON should either return a string (which would be the same in the env var) or throw an exception (for example for circular references). 但是在这里您却遇到了相反的情况,这很奇怪,很坦白地说,我无法针对我尝试过的任何值(例如NaNInfinity0''等)重现您的结果。序列化为JSON的其他所有内容都应该返回字符串(在env var中将是相同的)或引发异常(例如,对于循环引用)。

The only hint that comes to my mind would be hitting some length limit of the operating system - like trying to save a very long string resulting in not saving the string at all to the environment. 我想到的唯一提示是达到操作系统的某些长度限制-就像尝试保存一个很长的字符串导致根本不将字符串保存到环境中一样。

And now when I thought about it I searched for it and it seems that there's a limit of 32K characters on Windows, see: 现在,当我想到它时,我进行了搜索,并且看来Windows上的字符数限制为32K,请参阅:

The theoretical maximum length of an environment variable is around 32,760 characters. 环境变量的理论上最大长度约为32,760个字符。 However, you are unlikely to attain that theoretical maximum in practice. 但是,您不太可能在实践中达到该理论最大值。

Try printing the stringMock.length and it may answer your question. 尝试打印stringMock.length ,它可能会回答您的问题。

process.env values are implicitly converted to a string. process.env值隐式转换为字符串。 So for instance: 因此,例如:

> console.log('typeof process.env.MOCKS: ', typeof process.env.MOCKS);
typeof process.env.MOCKS:  undefined

> process.env.MOCKS = undefined
undefined
> process.env.MOCKS
'undefined'
> console.log('typeof process.env.MOCKS: ', typeof process.env.MOCKS);
typeof process.env.MOCKS:  string

So if you are trying to stringily something that returns undefined, you will end up assigning the string 'undefined' to process.env.MOCKS . 因此,如果您试图对返回未定义的内容进行stringily ,则最终将字符串'undefined'分配给process.env.MOCKS

As noted by rsp , the issue is related to the length of the string. rsp所述,问题与字符串的长度有关。 Sometimes stringMock has more than 40k characters. 有时stringMock具有超过40k个字符。

process.env.MOCKS = stringMock.substr(0, 30000);
console.log(process.env.MOCKS); // prints the string

process.env.MOCKS = stringMock.substr(0, 40000);
console.log(process.env.MOCKS); // prints undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM