Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load environment variables with ts-node?

I've tried a few implementations which none have been successful.

First Attempt

Using eval in package.json script "fetch:data": "eval $(cat .env) ts-node -O '{\"module\":\"commonjs\"}' ./bin/build-api-data.ts".

This results in a JSON parsing error because eval is removing my quotes for some reason.

undefined:1
{module:commonjs}
 ^

SyntaxError: Unexpected token m in JSON at position 1

Second Attempt

Using dotenv, the problem I encountered here was it was a race condition resulting in errors like this:

$ CANDID_ENV=local ts-node -O '{"module":"commonjs"}' ./bin/build-api-data.ts

/Users/lassiter.gregg/code/candidco-web/node_modules/contentful/dist/webpack:/contentful/contentful.js:49
    throw new TypeError('Expected parameter accessToken')
          ^
TypeError: Expected parameter accessToken

Code Sample

import fs from 'fs';
import path from 'path';
import fetchApiData from '../lib/apiData';
import dotEnv from 'dotenv-safe';

const { CANDID_ENV } = process.env;

const isLocalBuild = CANDID_ENV === 'local';

console.log(dotEnv);

const API_DATA_FILENAME = 'api_data.json';

const ensureDirectoryExistence = filePath => {
  var dirname = path.dirname(filePath);
  if (fs.existsSync(dirname)) {
    return true;
  }
  ensureDirectoryExistence(dirname);
  fs.mkdirSync(dirname);
};

const writeData = (filename, data) => {
  const filePath = path.join(__dirname, '..', '.data', filename);
  ensureDirectoryExistence(filePath);
  fs.writeFileSync(filePath, JSON.stringify(data));

  console.log('API data stored', filePath);
};

const fetchAndStoreApiData = async () => {
  console.log('Fetching all API data');
  await dotEnv.config({
    path: isLocalBuild ? './.env' : `./.env.${CANDID_ENV}`,
  });

  const newData = await fetchApiData();
  writeData(API_DATA_FILENAME, newData);
};

const init = async () => {
  fetchAndStoreApiData();
};

if (require.main === module) {
  init();
}

In the case above, I've tried doing dotenv.config at the top of the file, in the init, in the function as you see. It always throws the same error about contentful not getting the env variable it needs. That said, if I log process.env and comment out the code relevant to fetchApiData then I see all my environment variables. That's why I think it's a race-time condition but haven't been able to find anything similar to my own issue.

Additionally, what makes this even more thorny is that this is a custom script that has to work in a node and esnext environment. So, I've had my fair share of thorny import/export issues using syntax I don't really prefer but haven't found away around it (e.g. export = someFunction).

like image 702
jango Avatar asked Dec 28 '25 06:12

jango


1 Answers

You don't need another dependency like dotenv anymore. Now that you can specify an env file as an arg for node, I got it to work like so:

node --env-file=.env -r ts-node/register ./app.ts 

So if you have two files:

.myenv

SECRET=my_secret

and
test.ts

console.log("secret: ", process.env.SECRET);

You can run:

node --env-file=.myenv -r ts-node/register ./test.ts 

and see:

secret: my_secret
like image 184
user1160006 Avatar answered Dec 30 '25 18:12

user1160006