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

"Blog" example doesn't work with latest release #108

Open
dgknca opened this issue May 6, 2024 · 9 comments
Open

"Blog" example doesn't work with latest release #108

dgknca opened this issue May 6, 2024 · 9 comments

Comments

@dgknca
Copy link

dgknca commented May 6, 2024

./data.ts:15:101
Module not found: Can't resolve '.posts�uild-a-button-component-in-react.mdx'
13 | return b.frontMatter.date.getTime() - a.frontMatter.date.getTime()
14 | },

15 | }, {'C:\Users\dgknc\Desktop\mdxts\examples\blog\posts\build-a-button-component-in-react.mdx': () => import('.\posts\build-a-button-component-in-react.mdx')}, "{ title: string; date: Date; summary?: string; tags?: any; }")
| ^
16 |

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./app/page.tsx

Nodejs v20.11.0

@souporserious
Copy link
Owner

Thank you for filing an issue! It seems you are on a Windows machine, is that correct? I tried that version of Node.js on a Mac and was able to get the example working correctly. My assumption is the path is being constructed wrong on Windows which leads to the wrong import path from what I can tell in the error.

@dgknca
Copy link
Author

dgknca commented May 7, 2024

yes I use Windows.

@souporserious
Copy link
Owner

Thanks for confirming! I'll work on getting a VM set up when I get the chance and look into getting a fix out. From a quick look, I'm not sure exactly why the paths aren't being constructed properly (from the error at least) since I'm using sep here.

@dgknca
Copy link
Author

dgknca commented May 7, 2024

thanks for info. I'll try to debug it too.

@lanwin
Copy link

lanwin commented May 16, 2024

Hi, I stumble on the same problem while trying mdxts.

It seems here that the windows file API giving a path like this:

import('.\posts\build-a-button-component-in-react.mdx')

while I expect on linux/mac it should be resolve to this:

import('./posts/build-a-button-component-in-react.mdx')

It seems that import is expecting the later.

I simply put this in between and now I am a step further.

normalizedRelativePath = normalizedRelativePath.replaceAll("\\","/");

But now I get:

 ⨯ ./data.ts
TypeError: Assignment to constant variable.
    at Array.map (<anonymous>)
Import trace for requested module:

@Saeris
Copy link

Saeris commented May 17, 2024

I recently solved a similar file path resolution problem for Windows. The solution is rather simple:

import { join, posix, sep } from 'node:path';
import { pathToFileURL } from 'node:url';
import { globSync } from 'glob';

// Let's say you have a relative filepath, maybe from a glob library
// That filepath might have backslashes on Windows, and forward slashes on Unix

const root = process.cwd();
const path = './posts/*.mdx'

const files = await Promise.all(
  globSync(
    // Using node:path built-ins, we can normalize the filepath string to be Posix compliant
    join(root, path).split(sep).join(posix.sep)
  ).map(
    // Then, we'll use another built-in to convert a path like `C://path/to/file` to a `file://`
    // protocol path that `import()` understands
    (file) => import(pathToFileURL(file).toString())
  )
)

The important bits here are just replacing Windows path separators with Posix style separators, and then converting the resolved absolute path to use the file:// protocol so that import() can load it correctly.

This solution was informed by this Stack Overflow answer

@souporserious
Copy link
Owner

Thanks for the pointer @Saeris! I tried to interpret it as best I could in #130 and just published the changes so it's available in the latest release if someone in this thread would like to test it.

@Saeris
Copy link

Saeris commented May 22, 2024

@souporserious went and used bun create mdxts to scaffold a starter and this is what I got:

PS C:\Users\drake\GitHub\@saeris\mdxts-debug> bun dev
$ next
  ▲ Next.js 14.2.3
  - Local:        http://localhost:3000

 ✓ Starting...
   automatically enabled Fast Refresh for 1 custom loader
 ✓ Ready in 8.7s
 ○ Compiling / ...
 ⨯ ./data.ts:15:103
Module not found: Can't resolve './postuild-a-button-component-in-react.mdx'
  13 |   baseDirectory: 'posts',
  14 |   sort: (a, b) => b.frontMatter.date.getTime() - a.frontMatter.date.getTime(),
> 15 | }, {'C:\Users\drake\GitHub\@saeris\mdxts-debug\posts\build-a-button-component-in-react.mdx': () => import('./posts\build-a-button-component-in-react.mdx')}, "{ title: string; date: Date; summary?: string; tags?: any; }")
     |                                                                                                       ^
  16 |

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./app/page.tsx
 ⨯ ./data.ts:15:103
Module not found: Can't resolve './postuild-a-button-component-in-react.mdx'
  13 |   baseDirectory: 'posts',
  14 |   sort: (a, b) => b.frontMatter.date.getTime() - a.frontMatter.date.getTime(),
> 15 | }, {'C:\Users\drake\GitHub\@saeris\mdxts-debug\posts\build-a-button-component-in-react.mdx': () => import('./posts\build-a-button-component-in-react.mdx')}, "{ title: string; date: Date; summary?: string; tags?: any; }")
     |                                                                                                       ^
  16 |

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./app/page.tsx
   automatically enabled Fast Refresh for 1 custom loader
 ⨯ ./data.ts:15:103
Module not found: Can't resolve './postuild-a-button-component-in-react.mdx'
  13 |   baseDirectory: 'posts',
  14 |   sort: (a, b) => b.frontMatter.date.getTime() - a.frontMatter.date.getTime(),
> 15 | }, {'C:\Users\drake\GitHub\@saeris\mdxts-debug\posts\build-a-button-component-in-react.mdx': () => import('./posts\build-a-button-component-in-react.mdx')}, "{ title: string; date: Date; summary?: string; tags?: any; }")
     |                                                                                                       ^
  16 |

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./app/page.tsx
 GET / 500 in 11387ms

Still appears to be an issue with the import() statements having mixed slashes, such as import('./posts\build-a-button-component-in-react.mdx')

As an FYI, you can add an OS matrix in github actions to test on both Windows and Ubuntu. We did this to catch regressions over in Athena Crisis for this same category of issue.

A good smoke test would probably be something close to what I did here, just create a new app using the blog starter and attempt to start the server and navigate to the home page.

@souporserious
Copy link
Owner

Ah, ok I'll have to dig deeper when I get a chance. I took another stab at it in a83ed0e to hopefully fix that issue and normalize to posix.

Appreciate you taking a look! That's a great idea about setting up an action to test other operating systems.

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

4 participants