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

ESM not usable in Node #1927

Open
cprussin opened this issue Aug 21, 2023 · 3 comments
Open

ESM not usable in Node #1927

cprussin opened this issue Aug 21, 2023 · 3 comments
Assignees

Comments

@cprussin
Copy link

Currently this package is using the "module" package.json field, which is nonstandard and unsupported by node. As a result, node is unable to load ESM code and loads cjs code instead.

This package should define exports in the package.json to properly support ESM.

@3aluw
Copy link

3aluw commented Aug 30, 2023

You are right, I was facing an error using the ES6 syntax provided in the README : import contentful from "contentful-management"

@axe312ger axe312ger self-assigned this Feb 19, 2024
@axe312ger
Copy link
Collaborator

I'll take care of this :)

@marcogrcr
Copy link

It's worth noting that the current ESM output is only suitable for usage with a bundler for "tree-shaking" purposes. It is not a node-compatible ESM module. If you modify the current package.json as follows:

 {
    "name": "contentful-management",
    "version": "11.31.8",
    "description": "Client for Contentful's Content Management API",
    "homepage": "https://www.contentful.com/developers/documentation/content-management-api/",
    "main": "./dist/contentful-management.node.js",
    "browser": "./dist/contentful-management.browser.js",
    "types": "./dist/typings/contentful-management.d.ts",
    "module": "./dist/es-modules/contentful-management.js",
+   "type": "module",
+   "exports": {
+     "import": "./dist/es-modules/contentful-management.js",
+     "require": "./dist/contentful-management.node.js"
+   },

If you do the following:

index.mjs

import "contentful-management";

You'll get:

node:internal/modules/esm/resolve:264
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/(...)/node_modules/contentful-management/dist/es-modules/create-adapter' imported from /(...)/node_modules/contentful-management/dist/es-modules/contentful-management.js
    at finalizeResolution (node:internal/modules/esm/resolve:264:11)
    at moduleResolve (node:internal/modules/esm/resolve:924:10)
    at defaultResolve (node:internal/modules/esm/resolve:1148:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///(...)/node_modules/contentful-management/dist/es-modules/create-adapter'
}

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

4 participants