Skip to content

Commit e7264ee

Browse files
authored
fix: TwitterApiReadOnly and TwitterApiReadWrite named imports crashed on Node 22 (#649)
1 parent 7e98a0c commit e7264ee

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎.github/workflows/CI.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ jobs:
2727
- run: npm run build
2828
# Offline tests: the ones calling the real X API are run by hand, with the Live tests workflow
2929
- run: npm test
30+
# Installs the packed library and loads it with require() and import, like users do
31+
- run: node test/package-exports.mjs

‎src/client/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import TwitterApiv1 from '../v1/client.v1';
22
import TwitterApiv2 from '../v2/client.v2';
33
import { TwitterApiError } from '../types';
44
import TwitterApiReadWrite from './readwrite';
5+
import TwitterApiReadOnly from './readonly';
56
import TwitterAds from '../ads/client.ads';
67

78

@@ -84,6 +85,6 @@ export class TwitterApi extends TwitterApiReadWrite {
8485
}
8586
}
8687

87-
export { default as TwitterApiReadWrite } from './readwrite';
88-
export { default as TwitterApiReadOnly } from './readonly';
88+
// Not `export { default as … } from`: TypeScript compiles it to getters, that Node 22 and older don't see as named exports of a CommonJS module
89+
export { TwitterApiReadWrite, TwitterApiReadOnly };
8990
export default TwitterApi;

‎test/package-exports.mjs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Packs the library (run `npm run build` first), installs it in a temporary folder and loads it like users do:
2+
// every export of require('twitter-api-v2') must also be importable by name from an ES module.
3+
import { execFileSync } from 'child_process';
4+
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
5+
import { tmpdir } from 'os';
6+
import { join } from 'path';
7+
8+
const check = `
9+
import { createRequire } from 'module';
10+
11+
const required = createRequire(import.meta.url)('twitter-api-v2');
12+
const imported = await import('twitter-api-v2');
13+
const missing = Object.keys(required).filter(name => !(name in imported));
14+
15+
if (missing.length) {
16+
console.error('Exports that ES modules cannot import by name: ' + missing.join(', '));
17+
process.exit(1);
18+
}
19+
console.log('All ' + Object.keys(required).length + ' exports load with require() and import, on Node ' + process.version);
20+
`;
21+
22+
const folder = mkdtempSync(join(tmpdir(), 'twitter-api-v2-package-'));
23+
24+
try {
25+
const [{ filename }] = JSON.parse(execFileSync('npm', ['pack', '--json', '--pack-destination', folder], { encoding: 'utf8' }));
26+
writeFileSync(join(folder, 'package.json'), '{ "private": true }');
27+
execFileSync('npm', ['install', '--no-audit', '--no-fund', join(folder, filename)], { cwd: folder, stdio: 'inherit' });
28+
writeFileSync(join(folder, 'check.mjs'), check);
29+
execFileSync(process.execPath, ['check.mjs'], { cwd: folder, stdio: 'inherit' });
30+
} catch {
31+
process.exitCode = 1;
32+
} finally {
33+
rmSync(folder, { recursive: true, force: true });
34+
}

0 commit comments

Comments
 (0)