Skip to content

ERR_UNKNOWN_FILE_EXTENSION for tsconfig.json esm:true / NODE_OPTIONS works #1791

@zirkelc

Description

@zirkelc

Search Terms

ESM
NODE_OPTIONS
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

Expected Behavior

I'm using ts-node as part of Serverless to read the serverless.ts configuration file in my project which is defined as ESM with "type": "module" in package.json and "esm": true in tsconfig.json.

Same behavior like NODE_OPTIONS="--loader ts-node/esm".

Actual Behavior

If I start the serverless CLI with NODE_OPTIONS="--loader ts-node/esm", it loads the serverless.ts without a problem. However, if I avoid the NODE_OPTIONS env, I get the following error:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

ts-node is used programmatically and registered as require(tsNodePath).register(); and I can verify that the tsNodePath points to a locally installed package. Somehow, when it then comes to the following await import('serverless.ts') it crashes with this error.

Steps to reproduce the problem

Minimal reproduction

See repo: https://github.com/zirkelc/serverless-esm-test

Steps to reproduce:

  1. clone repo
  2. npm install
  3. npx sls package
    throws error ERR_UNKNOWN_FILE_EXTENSION]
  4. NODE_OPTIONS="--loader ts-node/esm" npx sls package
    no error

Specifications

ts-node v10.8.1
node v16.13.0
compiler v4.7.3

  • tsconfig.json, if you're using one:
{
  "extends": "@tsconfig/node16-strictest-esm/tsconfig.json",
  "compilerOptions": {
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
  },
  "include": [
    "src/**/*.ts",
    "serverless.ts"
  ],
  "exclude": [
    "node_modules/**/*",
    ".serverless/**/*",
    ".webpack/**/*",
    "_warmup/**/*",
    ".vscode/**/*"
  ],
  "ts-node": {
    "esm": true,
  }
}
  • package.json:
{
  "name": "serverless-esm-test",
  "version": "1.0.0",
  "description": "Serverless aws-nodejs-typescript template",
  "main": "serverless.ts",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "sls:package": "nodemon --ext ts --exec \"npx sls package\""
  },
  "engines": {
    "node": ">=14.15.0"
  },
  "dependencies": {
    "@middy/core": "^3.0.4",
    "@middy/http-json-body-parser": "^3.0.4"
  },
  "devDependencies": {
    "@serverless/typescript": "^3.18.0",
    "@tsconfig/node16-strictest-esm": "^1.0.1",
    "@types/aws-lambda": "^8.10.98",
    "@types/node": "^17.0.40",
    "esbuild": "^0.14.42",
    "json-schema-to-ts": "^2.5.3",
    "serverless": "github:zirkelc/serverless#add-esm-support-configuration",
    "serverless-esbuild": "^1.30.0",
    "ts-node": "^10.8.1",
    "tsconfig-paths": "^4.0.0",
    "typescript": "^4.7.3"
  },
  "author": "The serverless webpack authors (https://github.com/elastic-coders/serverless-webpack)",
  "license": "MIT"
}

  • Operating system and version:
  • If Windows, are you using WSL or WSL2?:
    macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions