Adobe API Mesh (Local Development setup using Github Codespaces) |
Additional resolvers |
Trying to import a .js file consisting of a very large mapping that is required in the additional resolver logic. |
Whenever trying to import a .js extension file inside the addtional resolver file , be it via commonjs or ES modules it is not supporting it. It throws an error: Error: LintError in File ./resolvers.js: Unexpected use of 'require'. require is › not supported |
Tried importing via both ES modules and CommonJS |
"@adobe/aio-cli": "^10.0.0", |
|
Views
Replies
Total Likes
Hi @LkKu ,
Use JSON Instead of .js for Large Mapping
1. Convert Large JS Mapping to JSON
Move the static mapping/data from your .js file to a .json file (e.g., mapping.json). JSON imports are always supported without require or import in Adobe's resolver context.
2. Load the JSON in the Resolver File
Use the standard synchronous fs.readFileSync (which is allowed since it’s not using require for code), or import JSON directly if the platform supports it:
const fs = require('fs');
const path = require('path');
// Load mapping synchronously at startup
const mapping = JSON.parse(fs.readFileSync(path.join(__dirname, 'mapping.json'), 'utf8'));
// Use this mapping in your functions as usual
If require('fs') is also blocked by the environment, load the JSON mapping as a static object directly in the resolver file as a last resort.
3. Don’t Use require for JS/TS Files
Move all logic into the main resolver file, or reference only .json for static data. Do not attempt require('./utils') or similar for other JS utility files in this context.
/api-mesh
mesh.json
resolvers.js <-- Only business logic
mapping.json <-- All large static data
Example resolvers.js:
const fs = require('fs');
const path = require('path');
const mapping = JSON.parse(fs.readFileSync(path.join(__dirname, 'mapping.json'), 'utf8'));
// Your resolver logic here, using mapping
module.exports = {
...
};
Views
Replies
Total Likes
Hi Amit, thanks for the quick response.
I had tried the JSON mapping idea with fs but require is just not allowed, and on prod the mapping is large and we have multiple files. I also thought of having a resolver build script that will replace mapping placeholders inside of resolver.js during build but large mappings might cause really slow start times or even memory overloading.
so the only feasible method that comes to mind now is to use some external data store to house these data and make fetch call using globalThis to retrieve it, might have to introduce batching for efficient retrieval.
something like this,
const fetchMapping = async () => {
const response = await globalThis.fetch('https://api.restful-api.dev/objects/1');
const data = await response.json();
return data;
}
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies