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

Presence of package.json causes autoload to throw Error: fastify-autoload cannot import plugin. #188

Closed
2 tasks done
thebearingedge opened this issue Aug 22, 2021 · 1 comment

Comments

@thebearingedge
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure it has not already been reported

Fastify version

3.20.2

Plugin version

3.8.0

Node.js version

16.4.2

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.4

Description

There seems to be a logic bug here that assumes indexDirent doesn't exist just because a package.json is present.

fastify-autoload/index.js

Lines 138 to 162 in ce2a45e

// Contains index file?
const indexDirent = list.find((dirent) => indexPattern.test(dirent.name))
if (indexDirent) {
const file = path.join(dir, indexDirent.name)
const type = getScriptType(file, options.packageType)
if (type === 'typescript' && !typescriptSupport) {
throw new Error(`fastify-autoload cannot import hooks plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`)
}
if (type === 'module' && !moduleSupport) {
throw new Error(`fastify-autoload cannot import hooks plugin at '${file}'. Your version of node does not support ES modules. To fix this error upgrade to Node 14 or use CommonJS syntax.`)
}
hookedAccumulator[prefix || '/'].plugins.push({ file, type, prefix })
const hasDirectory = list.find((dirent) => dirent.isDirectory())
if (!hasDirectory) {
return hookedAccumulator
}
}
// Contains package.json but no index.js file?
const packageDirent = list.find((dirent) => dirent.name === 'package.json')
if (packageDirent) {
throw new Error(`fastify-autoload cannot import plugin at '${dir}'. To fix this error rename the main entry file to 'index.js' (or .cjs, .mjs, .ts).`)
}

I'm working in a TypeScript monorepo and am trying to minimize directory nesting. Loading __dirname seemed to work, but when I started to use workspaces and added a package.json to my api/, the autoloader stopped working.

If I understand the intent behind the code, I think it should be this, seeing as we may not have returned on line 154.

  // Contains package.json but no index.js file?
  const packageDirent = list.find((dirent) => dirent.name === 'package.json')
  if (packageDirent && !indexDirent) {
    throw new Error(`fastify-autoload cannot import plugin at '${dir}'. To fix this error rename the main entry file to 'index.js' (or .cjs, .mjs, .ts).`)
  }

Steps to Reproduce

  • create a package.json in a directory
  • create an index.(j|t)s in that directory
  • try to autoload the directory

If you remove the package.json it works again.

Expected Behavior

The autoloader should not throw if there is an index file and a package file.

Given these files.

api/
├── auth
│   └── sign-in.ts
├── create-api.ts # export const autoload = false
├── index.ts      # export const autoload = false
├── package.json
├── tsconfig.json
└── typings
    └── fastify
        └── index.d.ts
  /* api/create-api.ts */
  await api.register(fastifyAutoload, {
    dir: __dirname
  })
@thebearingedge
Copy link
Contributor Author

I will get started on a PR, just in case.

thebearingedge added a commit to thebearingedge/fastify-autoload that referenced this issue Aug 22, 2021
don't assume that the presence of a package.json means the absence of an index file
thebearingedge added a commit to thebearingedge/fastify-autoload that referenced this issue Aug 22, 2021
don't assume that the presence of a package.json means the absence of an index file
Eomm pushed a commit that referenced this issue Aug 25, 2021
* fix: allow package.json in the root of autoload dir (#188)

don't assume that the presence of a package.json means the absence of an index file

* chore(deps): bump @types/jest from 26.0.8 to 27.0.1

Resolves conflicting peerOptional @types/jest@"^27.0.0" for [email protected].
@Eomm Eomm closed this as completed Aug 25, 2021
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