process.env在app.js外部不显示变量

app.js

    import koa from 'koa';
    import http from 'http';
    import logger from 'koa-logger';
    import koaBody from 'koa-body';
    import dotenv from 'dotenv';
    import dotenvExpand from 'dotenv-expand';

    const config = dotenv.config();

    dotenvExpand(config);

    console.log(process.env); // Here I see all data which are in my .env file

    import { client } from '@pg'; // Inside this file I doesn't see this,but it still after initializing dotenv

@pg = db / connection / index.js

import { Client } from 'pg';

console.log(process.env.DATABASE_URL,'fds'); // here I don't see the same ( all variables from .env file are undefined)

export const client = new Client({
  connectionString: process.env.DATABASE_URL
});

如果您需要其他信息,请告诉我。请注意我在代码段中的注释,这可能会有所帮助

zhuyanjunhunan 回答:process.env在app.js外部不显示变量

您需要在每个要调用dotenv变量的文件中调用.env

import dotenv from 'dotenv';
import { Client } from 'pg';

const config = dotenv.config();

console.log(process.env.DATABASE_URL,'fds');

如果您想在所有应用程序文件中调用dotenv而不是每次都调用,那么在运行应用程序时需要使用它:

node -r dotenv/config app.js
,

如果您不想使用外部软件包,则可以像这样运行脚本:) 我更喜欢这种方法。

@RunWith(AndroidJUnit4::class)
@LargeTest
class CameraTest: TestBuilder()
{
    @get:Rule
    var cameraActivityRule: ActivityTestRule<CameraActivity> = ActivityTestRule<CameraActivity>(CameraActivity::class.java,true,false)

    @get:Rule
    var permissionRule: GrantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.CAMERA,android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE)

    @Test
    fun cameraCaptureImageButtonIsInitiallySetWithCaptureImage() {
        this.cameraActivityRule.launchActivity(null)
        val imageButtonCapture: ImageButton = this.cameraActivityRule.activity.findViewById<ImageButton>(R.id.camera_image_btn_capture)
        Assert.assertEquals(imageButtonCapture.drawable,this.cameraActivityRule.activity.getDrawable(R.drawable.camera_capture))
    }
}
,

为了避免书写

dotenv.config()

在每个文件中,您只需在app.js

中添加这一行代码
dotenv.config({ path: path.resolve(__dirname,"path/to/.env") });
本文链接:https://www.f2er.com/2877492.html

大家都在问