Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support import.meta.env #127

Closed
pi0 opened this issue Mar 13, 2023 · 4 comments · Fixed by #129
Closed

Support import.meta.env #127

pi0 opened this issue Mar 13, 2023 · 4 comments · Fixed by #129

Comments

@pi0
Copy link
Member

pi0 commented Mar 13, 2023

#101 (comment)

@pi0
Copy link
Member Author

pi0 commented Mar 15, 2023

@pi0
Copy link
Member Author

pi0 commented Mar 15, 2023

Hmm i might drop some features like dotenv.

@kungpaogao
Copy link

kungpaogao commented Mar 31, 2023

EDIT: Nevermind part 2, I should've tried harder. I just needed to install dotenv

// index.ts
import * as dotenv from "dotenv";

dotenv.config();

console.log(import.meta.env.SECRET);

That said, still running into some funkiness with a file that looks like the following:

// index.ts
import { someFunction } from "./someFunction"

someFunction()

// someFunction.ts
...

const thirdParty = new ThirdPartyClient({ auth: import.meta.env.AUTH_TOKEN })

function someFunction() {
  thirdParty.something(...)
}

The above won't work because import.meta.env.AUTH_TOKEN will be undefined. I needed to import and config dotenv in someFunction.ts in order for this work.


EDIT: Nevermind, I see you have to reference a specific value, like import.meta.env.SECRET. However, this still returns undefined with the following:

// .env
SECRET=hello

// index.ts
console.log(import.meta.env.SECRET)
// > undefined

Thank you for adding this in! Unfortunately, I'm trying to use this right now, and I'm still getting SyntaxError: Cannot use 'import.meta' outside a module. Am I using this incorrectly?

Just running yarn jiti index.ts where index.ts is just console.log(import.meta.env).

@pi0
Copy link
Member Author

pi0 commented Mar 31, 2023

Yes, the way currently plugin works is that you need explicit import.meta.env.SOMETHING1 to be set. import.meta.env itself won't be transformed (we could support this btw..)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants