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

[Bug]: Can not use @aigne/core in commonjs scripts #36

Open
wangshijun opened this issue Mar 26, 2025 · 3 comments
Open

[Bug]: Can not use @aigne/core in commonjs scripts #36

wangshijun opened this issue Mar 26, 2025 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@wangshijun
Copy link
Contributor

Describe the bug

RT

Reproduction Steps

When run following scripts using node.js v20 or v22:

const { AIAgent, ClaudeChatModel, ExecutionEngine } = require('@aigne/core')
const model = new ClaudeChatModel({
  apiKey: process.env.ANTHROPIC_API_KEY,
  model: 'claude-3-5-sonnet-latest'
})
const engine = new ExecutionEngine({ model })

const agent = AIAgent.from({
  instructions: `\
You are a product analyst. Extract and summarize the key features of the product.

Product description:
{{product}}`,
  outputKey: 'features'
})

engine
  .call(agent, {
    product: 'AIGNE is a No-code Generative AI Apps Engine'
  })
  .then(result => {
    console.log(result)
  })
  .catch(error => {
    console.error(error)
  })

Observed Behavior

undefined
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/wangshijun/Develop/blocklet/aigne-code-reviewer/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@[email protected][email protected][email protected]_/node_modules/@aigne/core/lib/cjs/index.js from /Users/wangshijun/Develop/blocklet/aigne-code-reviewer/poc.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/wangshijun/Develop/blocklet/aigne-code-reviewer/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@[email protected][email protected][email protected]_/node_modules/@aigne/core/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at Object.<anonymous> (/Users/wangshijun/Develop/blocklet/aigne-code-reviewer/poc.js:1:55) {
  code: 'ERR_REQUIRE_ESM'
}

Expected Behavior

Should run as expected

Possible Solution

No response

Additional Information/Context

No response

@li-yechao
Copy link
Collaborator

@wangshijun
Copy link
Contributor Author

Related: blocklet/mcp-server-demo#2

@wangshijun
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants