#Getting error with generated api
8 messages · Page 1 of 1 (latest)
Hi @soft hearth. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
This error might be due to the way you're importing the api from the convex package. If you're not using "type": "module" in your package.json, Node.js will not be able to import the convex/_generated/api.js file directly. Instead, you'll need to use require() syntax.
You can configure Convex to generate a CommonJS version of the _generated/api.js file. To do this, in the same directory as your package.json, create or edit convex.json and add the following:
{
"generateCommonJSApi": true
}
After this, when the convex dev command generates files in convex/_generated/, a new api_cjs.cjs file will be created which can be imported from CommonJS code. Here's an example of how to import it:
const { api } = require("./convex/_generated/api_cjs.cjs");
If you're using TypeScript, you might want to add allowJs: true to your project-level tsconfig.json file.
If you're still facing issues, it might be due to the way your bundler is set up to resolve imports. If you have something like baseUrl: "." in a tsconfig.json, but your bundler is not actually set up to resolve all imports relative to that path, you might face a "Module not found" error. In such cases, try switching to relative imports and see if that works.
For more information, you can refer to the Convex documentation (https://docs.convex.dev/client/javascript/node#javascript-with-commonjs-require-syntax) and the support threads (https://discord.com/channels/1019350475847499849/1275080560586260491) on Discord.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@soft hearth kapa.ai is still learning and improving, please let us know how it did by reacting below
<@&1228088700710293556> I use type module but still need to put ../../../ etc in front to make work
@regal seal I use type module but still need to put ../../../ etc in front to make work
Let me check the knowledge base for you. One moment! :books:
If you're using "type": "module" in your package.json and still need to use relative paths like ../../../, it might be due to the way your TypeScript configuration is set up.
In your tsconfig.json, you can use the paths option to set up path aliases. This allows you to import modules in a more convenient way, without having to use relative paths. Here's an example:
{
"compilerOptions": {
"paths": {
"~/*": ["../*"],
"@cvx/*": ["./*"]
}
}
}
In this example, ~/* is an alias for the parent directory, and @cvx/* is an alias for the current directory. You can adjust these aliases to fit your project structure.
Please note that these path aliases are a TypeScript feature and might not be recognized by your JavaScript runtime or bundler. If you're using a bundler like Webpack or Parcel, you might need to configure the same aliases in your bundler configuration.