Skip to content

Commit 17fd3eb

Browse files
committed
fix: resolve CJS build issue causing "TypeError: Nylas is not a constructor" error
1 parent bc4bbb0 commit 17fd3eb

File tree

13 files changed

+340
-6
lines changed

13 files changed

+340
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
- Broken CJS build outputs resulted in a "TypeError: Nylas is not a constructor" error
12+
813
## [7.13.0] - 2025-09-01
914

1015
### Added

cjs-wrapper.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* TypeScript definitions for the CommonJS wrapper
3+
* This provides proper typing for CommonJS imports
4+
*/
5+
6+
// Re-export all types from the main module
7+
export * from './lib/types/models/index.js';
8+
9+
// Import the main Nylas class type
10+
import Nylas from './lib/types/nylas.js';
11+
12+
// Export as both default and named for maximum compatibility
13+
declare const nylasExport: typeof Nylas;
14+
export = nylasExport;

cjs-wrapper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* CommonJS wrapper for the Nylas SDK
3+
* This file provides CommonJS compatibility by re-exporting the default export
4+
* as the main module export while preserving named exports
5+
*/
6+
7+
const nylasModule = require('./lib/cjs/nylas.js');
8+
9+
// Export the default export as the main module export for CJS compatibility
10+
module.exports = nylasModule.default;
11+
12+
// Preserve all named exports
13+
Object.assign(module.exports, nylasModule);

examples/cjs-only/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# CommonJS-Only Nylas SDK Example
2+
3+
This example demonstrates how to use the Nylas Node.js SDK in a pure CommonJS (CJS) environment without ES module syntax.
4+
5+
## Purpose
6+
7+
- Shows CommonJS `require()` syntax with the Nylas SDK
8+
- Demonstrates environment variable handling in CommonJS
9+
- Provides a simple messages listing example
10+
- Serves as a reference for projects that must use CommonJS
11+
12+
## Key Differences from ESM
13+
14+
This example showcases the CommonJS equivalent of the ESM-only example:
15+
16+
| ESM Syntax | CommonJS Syntax |
17+
|------------|-----------------|
18+
| `import Nylas from 'nylas'` | `const Nylas = require('nylas')` |
19+
| `import dotenv from 'dotenv'` | `const dotenv = require('dotenv')` |
20+
| `import path from 'node:path'` | `const path = require('path')` |
21+
| `import.meta.dirname` | `__dirname` |
22+
| `export { logger }` | `module.exports = { logger }` |
23+
24+
## Setup
25+
26+
1. **Install dependencies:**
27+
```bash
28+
cd examples/cjs-only
29+
npm install
30+
```
31+
32+
2. **Set up environment variables:**
33+
- Copy `examples/.env.example` to `examples/.env`
34+
- Fill in your `NYLAS_API_KEY` and `NYLAS_GRANT_ID`
35+
36+
3. **Run the example:**
37+
```bash
38+
npm start
39+
# or
40+
node index.js
41+
```
42+
43+
## Requirements
44+
45+
- Node.js (any version - CommonJS is supported in all Node.js versions)
46+
- Valid Nylas API credentials
47+
- A grant with message access permissions
48+
49+
## What This Example Does
50+
51+
1. Loads environment variables using `dotenv`
52+
2. Validates required API credentials
53+
3. Initializes the Nylas client
54+
4. Lists messages from the specified grant
55+
5. Logs the results with proper error handling
56+
57+
## File Structure
58+
59+
```
60+
cjs-only/
61+
├── index.js # Main example file (CommonJS)
62+
├── package.json # Package configuration (no "type": "module")
63+
├── utils/
64+
│ └── logger.js # Logger utility (CommonJS exports)
65+
└── README.md # This file
66+
```
67+
68+
## Troubleshooting
69+
70+
- **"NYLAS_API_KEY environment variable is not set"**: Make sure you've created the `.env` file in the `examples/` directory with your API key
71+
- **"NYLAS_GRANT_ID environment variable is not set"**: Add your grant ID to the `.env` file
72+
- **Module not found errors**: Run `npm install` to install dependencies
73+
- **Permission errors**: Ensure your API key and grant have the necessary permissions to list messages
74+
75+
## Related Examples
76+
77+
- `../esm-only/` - ESM version of this same example
78+
- `../messages/` - More comprehensive message handling examples

examples/cjs-only/index.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* CommonJS-Only Nylas SDK Example
3+
*
4+
* This example demonstrates how to use the Nylas Node.js SDK in a pure CommonJS
5+
* (CJS) environment without ES module syntax.
6+
*
7+
* Purpose:
8+
* - Shows CommonJS require() syntax with the Nylas SDK
9+
* - Demonstrates environment variable handling in CommonJS
10+
* - Provides a simple messages listing example
11+
*
12+
* Usage:
13+
* 1. Copy the parent examples/.env.example to examples/.env
14+
* 2. Fill in your NYLAS_API_KEY and NYLAS_GRANT_ID in the .env file
15+
* 3. Run: node index.js (or npm start)
16+
*
17+
* Requirements:
18+
* - Node.js with CommonJS support (any Node.js version)
19+
* - Valid Nylas API credentials
20+
* - A grant with message access permissions
21+
*/
22+
23+
const dotenv = require('dotenv');
24+
const path = require('path');
25+
const Nylas = require('nylas');
26+
const { logger, maskSecret } = require('./utils/logger.js');
27+
28+
// Load from parent directory since this example lives in a subdirectory
29+
dotenv.config({ path: path.resolve(__dirname, '../.env') });
30+
31+
// Fail fast if credentials are missing to provide clear error messages
32+
const apiKey = process.env.NYLAS_API_KEY || '';
33+
if (!apiKey) {
34+
throw new Error('NYLAS_API_KEY environment variable is not set');
35+
}
36+
37+
const grantId = process.env.NYLAS_GRANT_ID || '';
38+
if (!grantId) {
39+
throw new Error('NYLAS_GRANT_ID environment variable is not set');
40+
}
41+
42+
// Initialize the Nylas client
43+
const nylas = new Nylas({
44+
apiKey,
45+
apiUri: process.env.NYLAS_API_URI || 'https://api.us.nylas.com',
46+
});
47+
48+
/**
49+
* Main function to demonstrate basic Nylas SDK usage in CommonJS environment
50+
*/
51+
async function main() {
52+
try {
53+
logger.info('Listing messages...');
54+
55+
// Log runtime config for debugging without exposing sensitive data
56+
logger.debug('Runtime config', {
57+
apiKey: maskSecret(apiKey),
58+
grantId,
59+
apiUri: process.env.NYLAS_API_URI || 'https://api.us.nylas.com',
60+
});
61+
62+
// Use basic list operation to verify SDK functionality and connectivity
63+
const messages = await nylas.messages.list({
64+
identifier: grantId,
65+
});
66+
67+
logger.success('Messages listed successfully');
68+
69+
// Extract only essential fields to avoid logging sensitive message content
70+
logger.info(
71+
'Message subjects and ids',
72+
messages.data.map((m) => ({ id: m.id, subject: m.subject }))
73+
);
74+
} catch (error) {
75+
logger.error('Failed to list messages');
76+
logger.debug('Error details', error);
77+
// Exit with error code to indicate failure for automation/CI purposes
78+
process.exit(1);
79+
}
80+
}
81+
82+
main();

examples/cjs-only/package-lock.json

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cjs-only/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "cjs-only",
3+
"version": "1.0.0",
4+
"description": "A CommonJS-only example of the Nylas NodeJS SDK usage",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node index.js"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"nylas": "file:../../",
14+
"dotenv": "^17.2.2"
15+
}
16+
}

examples/cjs-only/utils/logger.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Pretty logger for CommonJS
2+
const COLORS = {
3+
reset: '\x1b[0m',
4+
dim: '\x1b[2m',
5+
gray: '\x1b[90m',
6+
bold: '\x1b[1m',
7+
info: '\x1b[36m',
8+
success: '\x1b[32m',
9+
warn: '\x1b[33m',
10+
error: '\x1b[31m',
11+
debug: '\x1b[35m',
12+
};
13+
14+
function ts() {
15+
return new Date().toISOString();
16+
}
17+
18+
function maskSecret(value, visibleStart = 8, visibleEnd = 0) {
19+
if (!value) return '';
20+
const start = value.slice(0, visibleStart);
21+
const end = visibleEnd ? value.slice(-visibleEnd) : '';
22+
const hidden = Math.max(0, value.length - start.length - end.length);
23+
return `${start}${'•'.repeat(hidden)}${end}`;
24+
}
25+
26+
function print(level, symbol, message, meta) {
27+
const color = COLORS[level] || COLORS.info;
28+
const time = `${COLORS.dim}${ts()}${COLORS.reset}`;
29+
const label = `${color}${symbol} ${level.toUpperCase()}${COLORS.reset}`;
30+
const line =
31+
typeof message === 'string' ? message : JSON.stringify(message, null, 2);
32+
console.log(`${time} ${label} ${line}`);
33+
if (meta !== undefined) {
34+
console.dir(meta, { depth: null, colors: true, maxArrayLength: 100 });
35+
}
36+
}
37+
38+
const logger = {
39+
info: (msg, meta) => print('info', 'ℹ', msg, meta),
40+
success: (msg, meta) => print('success', '✔', msg, meta),
41+
warn: (msg, meta) => print('warn', '⚠', msg, meta),
42+
error: (msg, meta) => print('error', '✖', msg, meta),
43+
debug: (msg, meta) => print('debug', '🐛', msg, meta),
44+
};
45+
46+
module.exports = { logger, maskSecret };

examples/edge-environment/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@
7272
"url": "https://github.com/nylas/nylas-nodejs.git"
7373
},
7474
"exports": {
75-
"import": "./lib/esm/nylas.js",
76-
"require": "./lib/cjs/nylas.js",
77-
"types": "./lib/types/nylas.d.ts"
75+
"import": {
76+
"types": "./lib/types/nylas.d.ts",
77+
"default": "./lib/esm/nylas.js"
78+
},
79+
"require": {
80+
"types": "./cjs-wrapper.d.ts",
81+
"default": "./cjs-wrapper.js"
82+
}
7883
}
7984
}

0 commit comments

Comments
 (0)