KRusl
KRusl13mo ago

Error with imports

I have this fancy peace of code that import a bunch of files from a directory:
const utils = {};
const utilsDirectory = Deno.realPathSync(`${Deno.cwd()}/utils`);
const utilsFiles = Deno.readDirSync(utilsDirectory);

for (const utilsFile of utilsFiles) {
const filePath = `${utilsDirectory}/${utilsFile.name}`;
import(pathToUrl(filePath)).then(util => {
Object.assign(utils, util);
});
}
const utils = {};
const utilsDirectory = Deno.realPathSync(`${Deno.cwd()}/utils`);
const utilsFiles = Deno.readDirSync(utilsDirectory);

for (const utilsFile of utilsFiles) {
const filePath = `${utilsDirectory}/${utilsFile.name}`;
import(pathToUrl(filePath)).then(util => {
Object.assign(utils, util);
});
}
Structure of utils directory is:
- utils
- getTime.ts
- pathToUrl.ts
- someOtherFuncs.ts
- utils
- getTime.ts
- pathToUrl.ts
- someOtherFuncs.ts
When run this code using deno run everything is alright. But when I'm trying to deploy my app to Deno Deploy via deployctl deploy I receive this error:
× Deployment failed.
error: The deployment failed: UNCAUGHT_EXCEPTION

Uncaught (in promise) TypeError: module not found: 'file:///utils/getTime.ts'
× Deployment failed.
error: The deployment failed: UNCAUGHT_EXCEPTION

Uncaught (in promise) TypeError: module not found: 'file:///utils/getTime.ts'
Is there are something tricky about Deno Deploy FS? I don't get it why deno can't find this file.
4 Replies
Deno AI Helper
Deno AI Helper13mo ago
Yes, there is a difference in how Deno Deploy handles file system operations compared to the Deno runtime on your local machine. According to the documentation, Deno Deploy supports a limited set of the file system APIs available in Deno. These file system APIs can access static files from your deployments. Static files are, for example, the files in your GitHub repository if you deploy via the GitHub integration. In your case, the error message suggests that the file file:///utils/getTime.ts cannot be found. This might be due to the way paths are resolved in Deno Deploy. When you use Deno.cwd() in Deno Deploy, it returns the current working directory of your deployment, which is located at the root of your deployment’s root directory. If you deployed via the GitHub integration, the current working directory is the root of your GitHub repository. So, when you're trying to access the utils directory using Deno.realPathSync(${Deno.cwd()}/utils), it's looking for the utils directory at the root of your GitHub repository. If the utils directory is not at the root of your repository, this could be why the file cannot be found. To fix this, you should ensure that the path you're providing to Deno.realPathSync() correctly points to the utils directory in your repository. If the utils directory is not at the root of your repository, you'll need to include the necessary subdirectories in your path. For example, if your utils directory is inside a src directory at the root of your repository, you would use Deno.realPathSync(${Deno.cwd()}/src/utils). Remember that the paths should be relative to the root of your repository when deploying with Deno Deploy. 🦕
marvinh.
marvinh.13mo ago
The import specifiers must be statically analyzable for Deno Deploy, see https://deno.com/deploy/changelog#statically-analyzable-dynamic-imports
KRusl
KRuslOP13mo ago
Umm... Well, then it basically makes deno deploy a potato. Dynamic imports are very important and Deno Deploy just doesn't support it.
marvinh.
marvinh.13mo ago
support for dynamic imports from that perspective is definitely a bit more limited compared to what you're used to in deno deploy. Usually folks work around by doing some sort of code gen that generates files with statically analyzable imports