Add ES module support to fix import compatibility issues
This PR fixes the "Can not use import module" issue by adding proper ES module support to the package while maintaining full backward compatibility with CommonJS.
Problem
Users were unable to use ES6 import syntax in projects configured as ES modules (with "type": "module" in package.json). The package only supported CommonJS require() syntax, causing import errors in modern JavaScript environments.
Solution
Added an exports field to package.json that enables dual module support:
"exports": {
".": {
"import": "./index.js",
"require": "./index.js"
}
}
This tells Node.js how to handle both import styles:
-
CommonJS projects: Can continue using
require('streamed-chatgpt-api') -
ES module projects: Can now use
import { fetchStreamedChat } from 'streamed-chatgpt-api'
Verification
Added comprehensive tests to ensure both import methods work correctly:
- ✅ CommonJS
require()works in CommonJS projects - ✅ ES6
importworks in ES module projects (with"type": "module") - ✅ Dynamic imports work in both contexts
- ✅ All existing functionality preserved
- ✅ Backward compatibility maintained
Usage Examples
CommonJS (existing usage - unchanged):
const { fetchStreamedChat, fetchStreamedChatContent } = require('streamed-chatgpt-api');
ES Modules (now supported):
import { fetchStreamedChat, fetchStreamedChatContent } from 'streamed-chatgpt-api';
This is a minimal change (6 lines added) that resolves the import compatibility issue without affecting any existing functionality.
Fixes #7.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.