Open RATIU5 opened 1 month ago
For the time being, I setup a temporary fix using the following script to rewrite all the imports and exports to be explicit in defining the file name:
const fs = require("fs");
const path = require("path");
function fixImports(dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
fixImports(filePath);
} else if (file.endsWith(".js")) {
let content = fs.readFileSync(filePath, "utf8");
// Fix imports
content = content.replace(
/from\s+['"](\.[^'"]+)['"]/g,
(match, importPath) => {
const fullPath = path.resolve(path.dirname(filePath), importPath);
let relativePath = path
.relative(path.dirname(filePath), fullPath)
.replace(/\\/g, "/");
if (!relativePath.startsWith(".")) {
relativePath = "./" + relativePath;
}
if (fs.existsSync(fullPath + ".js")) {
return `from '${relativePath}.js'`;
} else if (fs.existsSync(path.join(fullPath, "index.js"))) {
return `from '${relativePath}/index.js'`;
}
return match;
},
);
// Fix exports
content = content.replace(
/export\s+.*from\s+['"](\.[^'"]+)['"]/g,
(match, exportPath) => {
const fullPath = path.resolve(path.dirname(filePath), exportPath);
let relativePath = path
.relative(path.dirname(filePath), fullPath)
.replace(/\\/g, "/");
if (!relativePath.startsWith(".")) {
relativePath = "./" + relativePath;
}
if (fs.existsSync(fullPath + ".js")) {
return match.replace(exportPath, relativePath + ".js");
} else if (fs.existsSync(path.join(fullPath, "index.js"))) {
return match.replace(exportPath, relativePath + "/index.js");
}
return match;
},
);
fs.writeFileSync(filePath, content);
}
});
}
const esmDir = path.join(
__dirname,
"..",
"node_modules",
"@medusajs",
"js-sdk",
"dist",
"esm",
);
fixImports(esmDir);
console.log("Fixed ESM imports and exports for @medusajs/js-sdk");
Any update on this issue? Been having problems in my storefront too.
@RATIU5 Thanks for the temporary fix, hoping for an update soon.
Bug report
Describe the bug
When installing and and using the
@medusajs/js-sdk
and write the code to setup a client, it throws anERR_UNSUPPORTED_DIR_IMPORT
error similar to the following:I got the same error using the following package managers:
System information
Medusa version (including plugins): Only
@medusajs/js-sdk
rc-4 Node.js version: 22.9.0 Database: Postgres 16 Operating system: macOS 15 Browser (if relevant): Arc (chromium)Steps to reproduce the behavior
"type": "module",
to yourpackage.json
@medusajs/js-sdk
package with version "rc"Expected behavior
The error simply should not appear, and the client should import successfully.
Screenshots
If applicable, add screenshots to help explain your problem
Code snippets
I tested this error without a framework on an empty npm project, with this code inside
run.js
:Then I ran
node run.js
to replicate the error.This is what the
package.json
looks like for this empty project:Additional context
My guess is that this has to do with the bundling/transpiling of the typescript to javascript in the js-sdk package.