Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module error in mjs version #77

Closed
MattInReality opened this issue Sep 10, 2022 · 2 comments
Closed

Module error in mjs version #77

MattInReality opened this issue Sep 10, 2022 · 2 comments

Comments

@MattInReality
Copy link

##Issue Description
When adding the plugin to a typescript Fastify project, I received a module import error on server start. The project was compiling to ES2022 (modules) and not CommonJs.

file-transfer-automation/node_modules/fastify-bree/lib/mjs/index.js:1
import FastifyPlugin from 'fastify-plugin';
^^^^^^

SyntaxError: Cannot use import statement outside a module

The issue is caused by a number of small issues:

  1. There is nothing to identify the files in the mjs directory as modules.

The following issues related to this block in lib\index.ts

import BreeClass, { BreeOptions } from 'bree'
import { FastifyPluginAsync } from 'fastify'
import FastifyPlugin from 'fastify-plugin'
import * as fs from 'fs'
import { BreeTS } from './plugin'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Bree = require('bree')
  1. Mixing import and require against Bree (modules can't use require)
  2. Modules require the .js extension on imports which is missing from the BreeTS plugin.4.

This lead me to a great blog post which all but handed the solution to me.

Having read it, I was writing a pull request but ran in to an issue where fixing MJS breaks it for ts-node. Correcting item 3 breaks ts-node tests unless compilation has been done as adding the extension makes a reference to a "non existent file" in ts-node. There is a heated discussion on the Typescript GitHub that you might be aware of where extension rewrites are completely ruled out despite modules requiring the .js extension.

This has all lead me to rework my project to CommonJs as ESM/TS is just filled with complication. Looking at 4.9 there is a bit about supporting .ts as an extension for modules like other run times do, though reading through I can't be confident compilation will replace .ts with .js like is needed in this instance.

The only thing that crossed my mind was splitting that Bree TS plugin out in to it's own NPM module and bringing it in to this package as a dependency.

@climba03003
Copy link
Owner

@MattInReality Sorry for the late reply. Can you check if version 3.0.3 fix your issue.

@climba03003
Copy link
Owner

Resolved import error in c0246a4
Resolved using require inside esm in 5a447a3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants