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

Windows clientModule returning null #84

Closed
2 tasks done
bskimball opened this issue Jul 4, 2022 · 3 comments
Closed
2 tasks done

Windows clientModule returning null #84

bskimball opened this issue Jul 4, 2022 · 3 comments

Comments

@bskimball
Copy link
Contributor

bskimball commented Jul 4, 2022

Prerequisites

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

Fastify version

3.29.0

Plugin version

3.0.0-beta.21

Node.js version

16.15.1

Operating system

Windows

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

10

Description

When running vue-stream example on Windows we receive an error:

TypeError: Cannot read properties of null (reading 'replace')

this comes from the following line in node_modules/fastify-vite/mode/development.js

const modulePath = resolve(config.vite.root, config.clientModule.replace(/^\/+/, ''))

it looks like the root of the client is returning null. On linux this same app works fine.

In order to fix this on Windows I changed the root in vite config to a relative path (i.e. "./client"). And in package.json, I change the "build:server" file to "./index.js"

After changing to relative urls, when I start the server I get

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

It may be related to this issue nodejs/node#37845

Steps to Reproduce

Copy vue-stream example to a windows machine and try to use

Expected Behavior

No response

@bskimball
Copy link
Contributor Author

bskimball commented Jul 5, 2022

What I've done is replace the vite config root with a relative url.
Then I changed the start script to "./server.js" instead of "server.js"

Looks like the error is thrown in the loadClient function in mode/production.js.

production.js line 53

const serverBundle = await import(resolve(config.bundle.dir, serverFile))

of course if I remove the dynamic import the app says it started but I get a 404 error when navigating to the home page

@bskimball
Copy link
Contributor Author

bskimball commented Jul 5, 2022

I have changed line 53 to use the file path for the dynamic import:

const serverBundle = await import(fileUrl(resolve(config.bundle.dir, serverFile)));
function fileUrl(str) {
  if (typeof str !== 'string') {
    throw new Error('Expected a string');
  }

  var pathName = resolve(str).replace(/\\/g, '/');

  // Windows drive letter must be prefixed with a slash
  if (pathName[0] !== '/') {
    pathName = '/' + pathName;
  }

  return encodeURI('file://' + pathName);
};

This obviously needs to be cleaned up and tested but this is working on Windows.

@bskimball
Copy link
Contributor Author

bskimball commented Jul 6, 2022

I created PR #85 in order to address these issues.

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

1 participant