Skip to content
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

Error with require() for ES Module in Node.js Environment #208

Open
cfdavidpetter opened this issue May 1, 2024 · 5 comments
Open

Error with require() for ES Module in Node.js Environment #208

cfdavidpetter opened this issue May 1, 2024 · 5 comments

Comments

@cfdavidpetter
Copy link

Issue Description:

When using the shpjs library in a Node.js environment (specifically version 21.1.0), an error occurs related to the use of require() for ES modules, resulting in the following error:

const shapefile = __importStar(require("shpjs"));

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/shpjs/lib/index.js from /app/dist/shared/infra/Shapefile.js not supported.
Instead change the require of index.js in /app/dist/shared/infra/Shapefile.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/app/dist/shared/infra/Shapefile.js:27:32) {
  code: 'ERR_REQUIRE_ESM'
}

Steps to Reproduce:

  1. Use the shpjs library in a Node.js environment (version 21.1.0).
  2. Attempt to import and use the library in a CommonJS module.
  3. Encounter the ERR_REQUIRE_ESM error when the module is loaded.

Expected Behavior:

The shpjs library should be usable in Node.js environments without encountering errors related to ES module syntax.

Environment:

Node.js version: 21.1.0
Operating System: Docker FROM node:21.1.0

Additional Information:

This issue was resolved by updating to version 4.0.4 of the shpjs library. However, it would be beneficial to ensure compatibility with older versions of Node.js or provide guidance on the supported Node.js versions in the library documentation.

@calvinmetcalf
Copy link
Owner

What version were you using when you got the error?

@cfdavidpetter
Copy link
Author

"shpjs": "5.0.0",

@calvinmetcalf
Copy link
Owner

you're already using typescript, just import it like an es module

@Plitzer
Copy link

Plitzer commented May 16, 2024

This should definitely be addressed. I can't use this in an AWS Lambda function for this exact reason.

Building from typescript results in a js file that always has require('shpjs') even if you use a dynamic import()

@calvinmetcalf
Copy link
Owner

import is the way the wind is blowing in node, I resisted it too but gave in. You can either use dynamic import or just use the old 4.x version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants