Giter Site home page Giter Site logo

Comments (25)

mcollina avatar mcollina commented on May 18, 2024 1

Unfortunately I do not know how to fix this. I'm moving this to fastify-autoload as a bug.

I might need some help from a Windows users here.. maybe @StarpTech or @Ethan-Arrowood?

from fastify-autoload.

StarpTech avatar StarpTech commented on May 18, 2024 1

Because in Linux a path is a valid file URL? 🤔

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024 1

@StarpTech can you open an issue on node core on this and tag me on it? Thanks. This is very confusing.

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024

I have to admit I have not tested that setup on Windows.
Can you please console.log(join(import.meta.url, 'routes'))?

from fastify-autoload.

amis-shokoohi avatar amis-shokoohi commented on May 18, 2024

C:\Users\amis\Desktop\fastify3-test\routes

from fastify-autoload.

Eomm avatar Eomm commented on May 18, 2024

The join from desm is doing this work?

import { dirname } from 'path';
import { fileURLToPath } from 'url';

const __dirname = dirname(fileURLToPath(import.meta.url)); ... path.join(__dirname, 'public'))) ...

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024

yes, that's the idea

from fastify-autoload.

amis-shokoohi avatar amis-shokoohi commented on May 18, 2024

I've actually tried that but still getting the same error.
I also went through fastify-autoload/index.js in debug mode, it seemed to work properly.
The problem is with esm. I rewrote it with cjs, it worked. I was just too excited to use esm.

from fastify-autoload.

fox1t avatar fox1t commented on May 18, 2024

This seems the same as this one nodejs/node#31710
Isn't this by design?
From what I am understanding we need to do the opposite of fileURLToPath (since in ESM context the absolute paths need to start with file://), so we need to use pathToFileURL.

from fastify-autoload.

StarpTech avatar StarpTech commented on May 18, 2024

How can I help? Will pathToFileURL solve the issue?

from fastify-autoload.

fox1t avatar fox1t commented on May 18, 2024

@StarpTech my bad, I am making some test now and import.meta.url indeed has already file://.

from fastify-autoload.

fox1t avatar fox1t commented on May 18, 2024

@StarpTech if you are a windows user just try to reproduce the issue using the provided apps.mjs and /routes/hello.js
It would be nice to know that is the file value at 117 line on windows.

from fastify-autoload.

StarpTech avatar StarpTech commented on May 18, 2024
console.log(join(import.meta.url, 'routes')); = "D:\repositories\fastify-autoload\route"

It would be nice to know that is the file value at 117 line on windows.v

D:\repositories\fastify-autoload\routes\hello.mjs

I could fix it with

fastify-autoloader/index.js

  if (type === 'module') {
    content = await import(url.pathToFileURL(file).href)
  } else {
    content = require(file)
  }

app.js

export function dirname(importMeta) {
    return path.dirname(filename(importMeta));
}

export function filename(importMeta) {
    return url.fileURLToPath(importMeta.url);
}

export default function (opts) {
    const app = fastify(opts)

    app.register(autoload, {
        dir: path.join(dirname(import.meta), 'routes')
    })


    return app
}

from fastify-autoload.

fox1t avatar fox1t commented on May 18, 2024

Nice, as supposed it worked! Now it would be nice to know why it was missing in the first place. Any clouds @mcollina ?

from fastify-autoload.

fox1t avatar fox1t commented on May 18, 2024

I think that since fastify-plugin handles ESM natively the users doesn't have to use desm package. 🤔

from fastify-autoload.

amis-shokoohi avatar amis-shokoohi commented on May 18, 2024

Oh thanks
So passing C:\...\hello.js to import() rather than file://C:/.../hello.js was the problem?

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024

Nice, as supposed it worked! Now it would be nice to know why it was missing in the first place. Any clouds @mcollina ?

No idea. The above code is exactly doing what desm is doing. See

https://github.com/mcollina/desm/blob/master/index.js

from fastify-autoload.

amis-shokoohi avatar amis-shokoohi commented on May 18, 2024

Can I steal @StarpTech code and send a PR? 😄

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024

sure. I don't 100% understand the problem, but +1

from fastify-autoload.

StarpTech avatar StarpTech commented on May 18, 2024

@amis-shokoohi feel free 😄

from fastify-autoload.

StarpTech avatar StarpTech commented on May 18, 2024

@mcollina the issue sits not there. We need to pass file URL to await import(), C:\ isn't one.

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024

why the heck this works on Mac and Linux then? :)

from fastify-autoload.

mcollina avatar mcollina commented on May 18, 2024

Released as v3.0.8!

from fastify-autoload.

StarpTech avatar StarpTech commented on May 18, 2024

Indeed, I tried it with WSL2 on Windows and /mnt/d/repositories/fastify-autoload/routes/hello.mjs is a valid file URL 😄 feels like a bug to me in Node.js. It's odd that the API doesn't abstract that behavior.

from fastify-autoload.

fox1t avatar fox1t commented on May 18, 2024

The Node.js parser takes c: part of the Windows paths as protocol. :/

from fastify-autoload.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.