Skip to content

Cannot load "serverless.ts": Initialization error - caused by incompatible tsconfig module setting that breaks serverless.ts compilation #48

Open
@justingrant

Description

@justingrant

I spent the last hour troubleshooting why serverless.ts was failing to load. The root cause was my tsconfig.json included "module": "esnext" which made the compiled severless.ts unable to be require-d by read.js. After a lot of googling and step-through debugging, I finally ended up at #28, which helped me identify a workaround: create a blank tsconfig file and use it for serverless.ts compilation using the TS_NODE_PROJECT environment variable. Like this:

TS_NODE_PROJECT=serverless-blank-tsconfig.json sls offline start

To avoid others having the same painful experience, here's some suggestions:

  • Could the TS_NODE_PROJECT workaround be documented somewhere other than Support tsconfig-paths and specify tsconfig*.json #28 ?
  • Could read.js override the module setting and any other tsconfig settings required for a successful compilation?
    AND/OR
  • Could read.js warn the user if their tsconfig has an incompatible setting?

For others who run across this error, here's a honeypot of Google keywords:

% sls offline start
 
 Serverless Error ----------------------------------------
 
  Cannot load "serverless.ts": Initialization error: /Users/justingrant/Documents/hdev/h3/api/serverless.ts:147
  export {};
  ^^^^^^
  
  SyntaxError: Unexpected token 'export'
      at compileFunction (<anonymous>)
      at Object.compileFunction (node:vm:352:18)
      at wrapSafe (node:internal/modules/cjs/loader:1025:15)
      at Module._compile (node:internal/modules/cjs/loader:1059:27)
      at Module.m._compile (/Users/justingrant/Documents/hdev/h3/api/node_modules/ts-node/src/index.ts:1225:23)
      at Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
      at Object.require.extensions.<computed> [as .ts] (/Users/justingrant/Documents/hdev/h3/api/node_modules/ts-node/src/index.ts:1228:12)
      at Module.load (node:internal/modules/cjs/loader:975:32)
      at Function.Module._load (node:internal/modules/cjs/loader:816:12)
      at Module.require (node:internal/modules/cjs/loader:999:19)
      at require (node:internal/modules/cjs/helpers:93:18)
      at /usr/local/lib/node_modules/serverless/lib/configuration/read.js:141:18
      at parseConfigurationFile (/usr/local/lib/node_modules/serverless/lib/configuration/read.js:156:9)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at module.exports (/usr/local/lib/node_modules/serverless/lib/configuration/read.js:183:23)
      at /usr/local/lib/node_modules/serverless/scripts/serverless.js:189:22
      at /usr/local/lib/node_modules/serverless/scripts/serverless.js:187:11
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              16.5.0
     Framework Version:         2.55.0
     Plugin Version:            5.4.3
     SDK Version:               4.2.6
     Components Version:        3.15.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions