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

Dynamic import path module not found. #6353

Closed
GroophyLifefor opened this issue Jan 20, 2025 · 1 comment
Closed

Dynamic import path module not found. #6353

GroophyLifefor opened this issue Jan 20, 2025 · 1 comment
Labels
question Further information is requested

Comments

@GroophyLifefor
Copy link

GroophyLifefor commented Jan 20, 2025

Describe the bug

Today I wanted to move my backend project(Nextjs APIs) to Deno with Oak.
In the structure I built using fs/walk.ts inside the std package, I need to import the files inside the “api” folder one by one and I do as.

import { walk } from 'https://deno.land/[email protected]/fs/walk.ts';

const cwd = Deno.cwd();
const endpoints: Array<{
  endpoint: {
    pattern: URLPattern;
    method: AllMethods;
  };
  handler: (ctx: ctx) => Response | Promise<Response>;
}> = [];

for await (const walkEntry of walk(cwd)) {
  const type = walkEntry.isSymlink
    ? 'symlink'
    : walkEntry.isFile
    ? 'file'
    : 'directory';

  console.log(type, walkEntry.path);

  if (type !== 'file') {
    continue;
  }

  // Format path and look, if not in api folder, continue
  const path = `.${walkEntry.path.substring(cwd.length).replaceAll('\\', '/')}`;
  if (!path.startsWith('./api')) {
    continue;
  }

  const actualPath = Deno.build.os === 'windows' 
  ? path
  : walkEntry.path;

  let pattern, GET, POST, PUT, DELETE, PATCH;
  try {
      ({ pattern, GET, POST, PUT, DELETE, PATCH } = await import(actualPath));
  } catch (error) {
    console.error(
      `Failed to import ${actualPath}`,
      error
    );
    continue;
  }
  // ...
}

started project and made root handler, logging, routing, models and more and worked so well, it's still working well in my local(which Windows filesystem), but when I wanted to deploy in deno deploy, can't use dynamic import properly because of ubuntu filesystem and says

Failed to import /src/api/Users/whoAmI.ts TypeError: module not found: 'file:///src/api/Users/whoAmI.ts'
    at async file:///src/endpoints.ts:62:53 {
  code: "ERR_MODULE_NOT_FOUND"
}

So I tried splitting operation system, tried so much way that possible to fix that, but nothing is worked.
Mine repository is public if you need to see all workspace: https://github.com/SwiftBuddiesTR/swiftbuddies-backend

Steps to Reproduce

  1. Looks to code that is problem,
  2. If not enough, you can clone repository,
  3. Or may you can create an simple environment that just dynamic importing file with walk and working fine in ubuntu.

Expected behavior

Importing ts file normally that how works in Windows.

Environment

Local

  • OS: Windows 11
  • Deno version: 2.1.6
  • std version: 0.170.0

Deno Deploy

  • OS: Unix* based any operation system that may ubuntu
  • Deno version: 2.1.6
  • std version: 0.170.0
@GroophyLifefor GroophyLifefor added bug Something isn't working needs triage labels Jan 20, 2025
@kt3k kt3k added question Further information is requested and removed bug Something isn't working needs triage labels Jan 21, 2025
@kt3k
Copy link
Member

kt3k commented Jan 21, 2025

dynamic import in deploy doesn't work with variables for security reason (You can only use dynamic-import with string literals https://docs.deno.com/deploy/api/dynamic-import/ )

The number of routes shouldn't be dynamic at runtime. I think you should use static import for importing routes.

@kt3k kt3k closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants