Comments (25)
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.
Because in Linux a path is a valid file URL? 🤔
from fastify-autoload.
@StarpTech can you open an issue on node core on this and tag me on it? Thanks. This is very confusing.
from fastify-autoload.
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.
C:\Users\amis\Desktop\fastify3-test\routes
from fastify-autoload.
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.
yes, that's the idea
from fastify-autoload.
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.
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.
How can I help? Will pathToFileURL
solve the issue?
from fastify-autoload.
@StarpTech my bad, I am making some test now and import.meta.url
indeed has already file://
.
from fastify-autoload.
@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.
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.
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.
I think that since fastify-plugin handles ESM natively the users doesn't have to use desm package. 🤔
from fastify-autoload.
Oh thanks
So passing C:\...\hello.js
to import()
rather than file://C:/.../hello.js
was the problem?
from fastify-autoload.
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.
Can I steal @StarpTech code and send a PR? 😄
from fastify-autoload.
sure. I don't 100% understand the problem, but +1
from fastify-autoload.
@amis-shokoohi feel free 😄
from fastify-autoload.
@mcollina the issue sits not there. We need to pass file URL to await import()
, C:\
isn't one.
from fastify-autoload.
why the heck this works on Mac and Linux then? :)
from fastify-autoload.
Released as v3.0.8!
from fastify-autoload.
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.
The Node.js parser takes c: part of the Windows paths as protocol. :/
from fastify-autoload.
Related Issues (20)
- babel-node + typescript. Error only with autoloads : throw new Error(@fastify/autoload cannot import plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.) HOT 5
- Add matchPattern to load only some specific paths HOT 2
- dirNameRoutePrefix option doesn't work when set to false HOT 2
- Mock plugins with autoload HOT 2
- Property '<property>' does not exist on type FastifyInstance when loading TS plugin HOT 1
- Two fastify instances, two `indexPattern` that seems been ignored HOT 2
- Autoload cannot read absolute paths from tsconfig throws error - "Error","message":"Cannot find module '@/Model/User/UserVerifyService' HOT 2
- Node 20 auto-loader not able to transpile TS to JS when using ts-node/esm HOT 24
- Params validation HOT 7
- Not able to have top-level await in a ts ESM file? HOT 3
- Fastify Autoload does not work with Bun.js or deno HOT 2
- Allow concatenation of child plugin prefixes when encapsulate is set to false HOT 2
- setNotFoundHandler is not encapsulated when called inside autohooks.js HOT 3
- autoloader breaks node --test --experimental-test-coverage HOT 2
- Cannot use with esm only HOT 1
- ts-node error with plugins using custom instance field via declaration merging HOT 1
- fileNameRoutePrefix HOT 3
- autoConfig dependencies not respected HOT 1
- Typescript, Vitetest, and Autoload HOT 10
- Achieve 100% test coverage HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-autoload.