[英]nestjs issue with variable in .env file
Im using NestJS framework to build a rest api, i have a issue getting the environment variables.我使用 NestJS 框架来构建一个 rest api,我在获取环境变量时遇到了问题。
I have a .env with a variable "SMB_SHARE" with a path string, when i pass this variable to a class constructor of a smb2 libray, this throw an error, the string provided by the environment variable is invalid.我有一个带有路径字符串变量“SMB_SHARE”的 .env,当我将此变量传递给 smb2 库的类构造函数时,这会引发错误,环境变量提供的字符串无效。
The environment variable is this:环境变量是这样的:
SMB_SHARE=\\\\10.00.0.000\\some_path SMB_SHARE=\\\\10.00.0.000\\some_path
When i use console log in the code the variable is ok, is a string and has the correct string value.当我在代码中使用控制台日志时,变量没问题,是一个字符串并且具有正确的字符串值。 But when i pass it to the constructor, it throw the error.
但是当我将它传递给构造函数时,它会抛出错误。
I pass the string directly to the constructor, and it work fine, the others environment variables of the constructor are setted correctly (like the username and password).我将字符串直接传递给构造函数,它工作正常,构造函数的其他环境变量设置正确(如用户名和密码)。 Only the SMB_SHARE variable is throwing the error.
只有 SMB_SHARE 变量抛出错误。
I dont have idea what is the problem here.我不知道这里有什么问题。 Can someone help me with this issue?
有人可以帮我解决这个问题吗?
I show some examples:我展示了一些例子:
constructor(private config: ConfigService) {
console.log('VAR', config.get<string>('SMB_SHARE'));
//This show the correctly string variable value
const share = config.get<string>('SMB_SHARE');
this.sambaClient = new SMB2({
share: '\\\\10.00.0.000\\some_path', //WORK
//share: share, FAIL
//share: config.get<string>('SMB_SHARE'), FAIL
//share: process.env.SMB_SHARE, FAIL
domain: '',
username: config.get<string>('SMB_USERNAME'),
password: config.get<string>('SMB_PASSWORD'),
debug: true,
autoCloseTimeout: 0,
})
}
The .env file is like this: .env 文件是这样的:
SMB_SHARE=\\\\10.00.0.000\\some_path
SMB_USERNAME=user
SMB_PASSWORD=secret
More than likely, what is happening is JavaScript is escaping the extra \\
.更有可能的是,正在发生的事情是 JavaScript 正在逃避额外的
\\
。 This is unescaped when the print happens, so it looks proper (ie console.log(process.env.SMB_SHARE)
will print \\\\\\\\10.0.0.0\\\\some_path
, but in reality, the variable is now \\\\\\\\\\\\\\\\10.0.0.0\\\\\\\\some_path
).这是转义当打印发生,所以它看起来正确的(即
console.log(process.env.SMB_SHARE)
将打印\\\\\\\\10.0.0.0\\\\some_path
,但在现实中,变量是现在\\\\\\\\\\\\\\\\10.0.0.0\\\\\\\\some_path
)。 I ended up creating a mock of this using a text file called ./temp/.env
and making use of the fs
module from Node (which is what dotenv
uses, which is what @nestjs/config
uses. You can see below the cat
(print) of the file, and the two different methods while using node
to read the file我最终使用一个名为
./temp/.env
的文本文件创建了一个模拟,并使用了来自 Node 的fs
模块(这是dotenv
使用的,这是@nestjs/config
使用的。你可以在cat
下面看到(print) 文件,以及使用node
读取文件时的两种不同方法
~/Documents/code
▶ cat ./temp/.env
HOST=\\\\127.0.0.1:3000\\path
~/Documents/code
▶ node
Welcome to Node.js v12.18.2.
Type ".help" for more information.
> const { readFileSync } = require('fs');
undefined
> readFileSync('./temp/.env').toString()
'HOST=\\\\\\\\127.0.0.1:3000\\\\path\n\n'
> console.log(readFileSync('./temp/.env').toString())
HOST=\\\\127.0.0.1:3000\\path
The solution here, would be to change your .env
file to be the exact string you're wanting to pass on to the configuration (probably \\\\10.0.0.0\\some_path
)这里的解决方案是将您的
.env
文件更改为您想要传递给配置的确切字符串(可能是\\\\10.0.0.0\\some_path
)
you have to implement the config module.你必须实现配置模块。
start with从...开始
npm i --save @nestjs/config
npm i --save @nestjs/config
then add the configModule in your appModule:然后在你的 appModule 中添加 configModule:
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [ConfigModule.forRoot()],
})
export class AppModule {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.