Skip to content

ES Modules + auto-load + node-ts #1014

Closed
@melroy89

Description

@melroy89

💬 Question/problem here

In issue #309 of autoloader which I try to follow (eg. FASTIFY_AUTOLOAD_TYPESCRIPT) @climba03003 mentioned that if you still have issues, you configured your project incorrectly.

If anything is not working. It is due the configuration problem on your environment.

However, I'm pretty sure my code works without fastify-autoload on routes directory. And I'm also sure I use type": "module" in package.json and "module": "NodeNext" in tsconfig.json with target ES2022 and ts-node module configuration with:

  "ts-node": {
    "files": true,
    "esm": true,
  },

And even using the FASTIFY_AUTOLOAD_TYPESCRIPT=1 flag like so:

FASTIFY_AUTOLOAD_TYPESCRIPT=1 node --no-warnings=ExperimentalWarning --loader ts-node/esm src/index.ts

npm start which will use tsc will work fine!

However running the command above (for example via npm run dev), this results into:

[nodemon] starting `npm run develop`

> [email protected] develop
> FASTIFY_AUTOLOAD_TYPESCRIPT=1 node --no-warnings=ExperimentalWarning --loader ts-node/esm src/index.ts

{
  "level": 50,
  "time": 1710524606541,
  "pid": 49678,
  "hostname": "melroy-pc",
  "err": {
    "type": "SyntaxError",
    "message": "Cannot use import statement outside a module at /media/melroy/Data/Projects/fastify_esm_ts_node/src/routes/index.ts:1",
    "stack": "/media/melroy/Data/Projects/fastify_esm_ts_node/src/routes/index.ts:1\nimport { FastifyPluginAsync } from 'fastify'\n^^^^^^\n\nSyntaxError: Cannot use import statement outside a module\n    at internalCompileFunction (node:internal/vm:77:18)\n    at wrapSafe (node:internal/modules/cjs/loader:1288:20)\n    at Module._compile (node:internal/modules/cjs/loader:1340:27)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)\n    at Module.load (node:internal/modules/cjs/loader:1207:32)\n    at Module._load (node:internal/modules/cjs/loader:1023:12)\n    at Module.require (node:internal/modules/cjs/loader:1235:19)\n    at require (node:internal/modules/helpers:176:18)\n    at loadPlugin (/media/melroy/Data/Projects/fastify_esm_ts_node/node_modules/@fastify/autoload/index.js:273:15)\n    at /media/melroy/Data/Projects/fastify_esm_ts_node/node_modules/@fastify/autoload/index.js:44:12"
  },
  "msg": "Cannot use import statement outside a module at /media/melroy/Data/Projects/fastify_esm_ts_node/src/routes/index.ts:1"
}

[nodemon] app crashed - waiting for file changes before starting...

Demo GitHub repository for the reproduction code: https://github.com/melroy89/fastify-node-ts-issue
Demo GitHub repository that now contains the workaround VITEST: https://github.com/melroy89/fastify-node-ts-swc-demo

Code in question:

    // Load routes
    this.app.register(autoLoad, {
      dir: path.join(__dirname, 'routes'),
    })

So you can test it for yourself, again see github repo above.
Please some help or advise would be much appropriated! ❤️ . I would like to integrate Fastify further into my applications and still being able to use node-ts for rapid software development.

Your Environment

  • node version: 20.11.1
  • fastify version: >=4.26.2
  • os: Linux Mint 21.3
  • tsc version: 5.2.0
  • ts-node version: >=10.9.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions