Unexpected token 'export' from formdata-node when using openai/shims/node
Problem
Confirm this is a Node library issue and not an underlying OpenAI API issue - [X] This is an issue with the Node library Describe the bug Issues experienced while running unit tests in our nodejs backend. I first encountered the fetch is not defined bug, which led me to https://github.com/openai/openai-node/issues/304 and was able to move past the issue with `import 'openai/shims/node'` But now I'm receiving the following errors: [code block] I've also added [code block] But I'll still get the same `SyntaxError: Unexpected token 'export'` error. I've updated the `transformIgnorePatterns` value to include `"/node_modules/(?!formdata-node)"` as well as `"/node_modules/(?!openai/node/shims)"` and many combinations. Still no luck. yarn v1.22.4 typescript v5.1.6 jest v29.7.0 To Reproduce `yarn add openai` I've been able to reproduce it with as little as just the imports. Add the following imports to a file that has unit tests (or a logic/utils file used by a file being tested) [code block] run the tests on that file Code snippets _No response_ OS macOS Node version Node v16.20.0 (also tested with v18.19.0 same results) Library version openai v4.20.0
Error Output
Error: Unexpected token 'export'
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Resolve 'Unexpected token export' Error in Jest Tests
The error 'Unexpected token export' occurs because Jest is trying to parse ES module syntax in the 'formdata-node' package, which is not transpiled correctly for CommonJS environments. This is compounded by the fact that Jest's default behavior is to ignore certain node modules unless explicitly configured to transform them.
Awaiting Verification
Be the first to verify this fix
- 1
Update Jest Configuration
Modify the Jest configuration to ensure that both 'formdata-node' and 'openai/shims/node' are properly transformed. This will allow Jest to handle ES module syntax correctly.
javascriptmodule.exports = { transformIgnorePatterns: ["/node_modules/(?!formdata-node|openai/shims)"] }; - 2
Install Babel for Jest
If not already installed, add Babel to your project to transpile ES modules. This will help Jest understand the syntax used in the imported libraries.
bashyarn add --dev @babel/preset-env babel-jest - 3
Create Babel Configuration
Create or update the Babel configuration file to include the preset for transforming ES modules. This ensures that Jest can process the imports correctly.
json{ "presets": ["@babel/preset-env"] } - 4
Run Jest Tests
After making the above changes, run your Jest tests again to check if the issue is resolved.
bashyarn test - 5
Verify Imports
Ensure that your imports in the test files are correctly referencing the modules. If you are using ES module syntax, make sure your Node environment supports it or is properly configured.
typescriptimport { YourFunction } from 'your-module';
Validation
Confirm that the Jest tests run without throwing the 'Unexpected token export' error. Additionally, check that all expected test cases pass successfully.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep