Why when using ts-node am I getting an Uncaught SyntaxError: Unexpected token 'export'?

411 Views Asked by At

I'm trying to do something like the following, and these are the contents of ./env/config referenced below:

import path from 'path';
export default await import(`./${process.env.NODE_ENV || 'development'}`).then((env) => {
  const defaults = {
    root: path.join(path.dirname('.'), '/..'),
    api: {
      title: 'API: Users and Application Resources',
      version: '0.0.1',
      description: 'A RESTful API for managing users and application resources.',
    },
    fileSizeLimit: 10 * 1024 * 1024,
  };

  return Object.assign(env, defaults);
});

But I'm getting the error mentioned above when I run npx ts-node and then type in the REPL import config from './config/env'

Here's my tsconfig:

{
  "compilerOptions": {
    /* Docs: [https://www.typescriptlang.org/tsconfig](https://www.typescriptlang.org/tsconfig) */
    "module": "ES2022",
    "target": "ES2022",
    "outDir": "bin",
    "rootDir": "/",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "checkJs": true,
    "allowJs": true,
    "moduleResolution": "node",
    "types": ["node", "mocha", "chai"],
    "typeRoots": ["node_modules/@types"],
    "lib": ["ES2022"],
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "allowUnreachableCode": true,
    "useUnknownInCatchVariables": false // TypeScript 4.4+ only!
  },
  "ts-node": {
    "esm": true,
    "compilerOptions": {
      "module": "ES2022"
    }
  },
  "include": [
    "app/**/*.ts",
    "config/**/*.ts",
    "test/**/*.ts"
  ],
  "exclude": ["node_modules", "<node_internals>/**", "./built/**/*"]
}

What do I need to change to make my import work?

Note: This error also occurs when I just try to import dotenv from 'dotenv';

0

There are 0 best solutions below