Skip to content

CommonJS not working since jira.js v4.0.5 #387

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

Open
deepwell opened this issue May 28, 2025 · 9 comments
Open

CommonJS not working since jira.js v4.0.5 #387

deepwell opened this issue May 28, 2025 · 9 comments
Assignees
Labels
Investigation required Points out issues that need further exploration or detailed examination.

Comments

@deepwell
Copy link

Since the migration from mime-types to mime in v4.0.5, the CommonJS build of jira.js is no longer working.
It would be great to support CommonJS builds again for deploying to locations where ESM is not supported (eg: AWS Lambda).

@MrRefactoring
Copy link
Owner

Thanks for reporting this issue! To help us investigate the CommonJS compatibility problem after the migration to mime, could you please provide the following details?

  1. The version of jira.js you’re using.
  2. Your Node.js version.
  3. A small code example where this fails (e.g., a simple require statement or usage snippet).
  4. The exact error message you’re encountering.

For example:

// Example that fails in CommonJS  
const { JiraClient } = require('jira.js');  
// ...  

This will help us reproduce the issue and work on a solution. If you’re using a bundler (like Webpack) or a specific Lambda runtime, mentioning that would also be useful.

@MrRefactoring MrRefactoring self-assigned this May 28, 2025
@MrRefactoring MrRefactoring added the Investigation required Points out issues that need further exploration or detailed examination. label May 29, 2025
@deepwell
Copy link
Author

deepwell commented Jun 3, 2025

I'm using jira.js v5.2.0
node.js version, I set the AWS Lambda to node.js 22.x and it's reporting nodejs:22.v43. Must be an AWS version.

Code where it's failing to load the jira.js library with first use of the library:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jira_js_1 = require("jira.js");
function jiraCloud({ atlassianEmail, atlassianApiToken }) {
    const client = new jira_js_1.Version3Client({
        host: 'https://kritikio.atlassian.net',
        authentication: {
            basic: {
                email: atlassianEmail,
                apiToken: atlassianApiToken,
            },
        },
    });
...

This is partial uncompiled TypeScript source:

import { Version3Client } from 'jira.js';
...

Error message:

{
  "errorType": "Error",
  "errorMessage": "require() of ES Module /var/task/node_modules/mime/dist/src/index.js from /var/task/node_modules/jira.js/dist/cjs/createClient.cjs not supported.\nInstead change the require of index.js in /var/task/node_modules/jira.js/dist/cjs/createClient.cjs to a dynamic import() which is available in all CommonJS modules.",
  "trace": [
    "Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/node_modules/mime/dist/src/index.js from /var/task/node_modules/jira.js/dist/cjs/createClient.cjs not supported.",
    "Instead change the require of index.js in /var/task/node_modules/jira.js/dist/cjs/createClient.cjs to a dynamic import() which is available in all CommonJS modules.",
    "    at TracingChannel.traceSync (node:diagnostics_channel:322:14)",
    "    at Object.<anonymous> (/var/task/node_modules/jira.js/dist/cjs/createClient.cjs:5:1)"
  ]
}

This is a TypeScript code base and I'm using tsc for the build process to product JavaScript in CommonJS format for AWS Lambda.

@MrRefactoring
Copy link
Owner

Can I ask you to create a basic playground that I could deploy on AWS (no pipeline needed)? Because I still suspect that you have issues with the project settings

@deepwell
Copy link
Author

deepwell commented Jun 4, 2025

Here you go: https://github.com/deepwell/lambda-jirajs
All you need to do is run:

npm install
npm run package

Then upload deployments.zip to a new AWS Lambda. Use the Test feature to run the code.
No need to set environment variables or correct Atlassian config.

@MrRefactoring
Copy link
Owner

Could you please try this changes in package.json:

{
  "name": "jira-reproduce",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "package": "npm run build && cp package.json dist && cd dist && npm install --omit=dev && zip -r ../deployment.zip .",
    "test": "jest",
    "type-check": "tsc --build ./tsconfig.json"
  },
  "author": "",
  "dependencies": {
    "jira.js": "5.2.0"
  },
  "devDependencies": {
    "@types/aws-lambda": "8.10.149",
    "@types/node": "22.15.18",
    "typescript": "5.8.3"
  }
}

@deepwell
Copy link
Author

deepwell commented Jun 6, 2025

I applied that change, rebuild the package, uploaded it, re-tested, and this is what it returns now:

{
  "errorType": "ReferenceError",
  "errorMessage": "exports is not defined in ES module scope\nThis file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
  "trace": [
    "ReferenceError: exports is not defined in ES module scope",
    "This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
    "    at file:///var/task/index.js:2:23",
    "    at ModuleJob.run (node:internal/modules/esm/module_job:274:25)",
    "    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:644:26)",
    "    at async _tryAwaitImport (file:///var/runtime/index.mjs:1013:16)",
    "    at async _tryRequire (file:///var/runtime/index.mjs:1062:37)",
    "    at async _loadUserApp (file:///var/runtime/index.mjs:1086:16)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1131:21)",
    "    at async start (file:///var/runtime/index.mjs:1300:23)",
    "    at async file:///var/runtime/index.mjs:1306:1"
  ]
}

In tsconfig.json, I changed "module": "es2022", and then it worked 🥳

@MrRefactoring
Copy link
Owner

Am I correct in understanding that the issue has been resolved and I can close it?

@holatuwol
Copy link

I'm working on a project that compiles to commonjs to support another ticketing system (zendesk via node-zendesk), and when we tried to include this project to interact with Jira, the test framework (jest) errored out when it got to the mime module that's brought in by this project.

SyntaxError: Cannot use import statement outside a module

Since this thread describes a workaround, I was wondering if a similar workaround would apply in my case. I've created a minimal example that will break if you run nvm use && npm i && npm run test

https://github.com/holatuwol/jira.js/tree/issue-387

@MrRefactoring
Copy link
Owner

If you change the target here to ES6 instead of ES2022, will it work?

https://github.com/holatuwol/jira.js/blob/issue-387/tsconfig.json#L3

Or you could add "type": "module" to package.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Investigation required Points out issues that need further exploration or detailed examination.
Projects
None yet
Development

No branches or pull requests

3 participants