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

import.meta.url workaround example #2

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

thescientist13
Copy link
Owner

@thescientist13 thescientist13 commented Feb 10, 2025

testing work arounds to using import.meta.url 🤞

Attempts

Copy link

cloudflare-workers-and-pages bot commented Feb 10, 2025

Deploying cloudflare-workers-demo with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7fe4d65
Status:🚫  Build failed.

View logs

@thescientist13
Copy link
Owner Author

thescientist13 commented Feb 10, 2025

Tried this

import path from 'path';

const fakeImportMetaUrl = `file://${path.dirname(process.argv[1])}/`;
const dataUrl = new URL('../../public/data.json', fakeImportMetaUrl);

console.log({ fakeImportMetaUrl, dataUrl });

But still got an error

14:32:38.136 | ✨ Success! Uploaded 0 files (2 already uploaded) (0.40 sec)
-- | --
14:32:38.136 |  
14:32:38.359 | ✨ Upload complete!
14:32:41.115 | Success: Assets published!
14:32:42.162 | Error: Failed to publish your Function. Got error: Uncaught TypeError: The "path" argument must be of type string. Received undefined   at node-internal:validators:101:15 in validateString   at node-internal:internal_path:1119:9 in dirname   at functionsWorker-0.701045628589601.js:986:40

@thescientist13
Copy link
Owner Author

Trying this approach to extract the information from a Stack trace

function getCurrentDirectory() {
  const error = new Error();
  Error.captureStackTrace(error);
  const stack = error.stack;

  console.log({ stack});
  const callerLine = stack.split('\n')[2];
  console.log({ callerLine });
  const callerFile = callerLine.slice(callerLine.indexOf('file://')); // match(/file:\/\//)[0];
  console.log({ callerFile });
  const currentDir = `file:${path.dirname(callerFile.split(':')[1])}`; // path.dirname(callerFile);

  console.log({ currentDir });
  return currentDir;
}
14:49:37.844 | ✨ Compiled Worker successfully
-- | --
14:49:38.930 | Validating asset output directory
14:49:47.600 | Success: Assets published!
14:49:48.563 | Error: Failed to publish your Function. Got error: Uncaught TypeError: The "path" argument must be of type string. Received undefined   at node-internal:validators:101:15 in validateString   at node-internal:internal_path:1119:9 in dirname   at functionsWorker-0.7477680660376456.js:995:35 in getCurrentDirectory   at functionsWorker-0.7477680660376456.js:1000:25

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

Successfully merging this pull request may close these issues.

1 participant