contentful / contentful-management.js

JavaScript library for Contentful's Management API (node & browser)
https://contentful.github.io/contentful-management.js
MIT License
266 stars 97 forks source link

ESM not usable in Node #1927

Open cprussin opened 1 year ago

cprussin commented 1 year ago

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 commented 1 year ago

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

axe312ger commented 6 months ago

I'll take care of this :)

marcogrcr commented 2 weeks ago

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'
}