Is there a way to import TS vars file in serverless.yml?

aws

#1

It’s a huge config object and I wanna reuse it within my typescript app. But serverless only supports js files, and I don’t wanna lose the config type information.

${file(./config.js)}


#2

I did it by requiring typescript from a Javascript file like this:

module.exports = () => {
  require('ts-node').register({module: "commonjs"});
  require('tsconfig-paths').register();
  return require('./my-typescript.ts');
};

Update:

I discovered that this approach can cause problems when using serverless offline, as it invokes the expensive .register() method every time you invoke a lambda function. I found moving the require outside helps:

require('ts-node').register({module: "commonjs"});
require('tsconfig-paths').register();

module.exports = () => {
  return require('./my-typescript.ts');
};