Skip to content

πŸ› Bug Report: Packages with esm output fail when imported into node modulesΒ #19854

Closed as not planned
@davidscodex

Description

πŸ“œ Description

Importing backstage packages with esm output breaks in a node module.

Without "type": "module" specified in the nearest package.json, node treats all .js files as commonjs. See https://nodejs.org/api/packages.html#determining-module-system

πŸ‘ Expected behavior

Import successfully.

πŸ‘Ž Actual Behavior with Screenshots

file:///Users/davidsullivan/projects/git/backstage-module-failure/index.js:1
import { entitySchemaValidator } from "@backstage/catalog-model";
         ^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'entitySchemaValidator' not found. The requested module '@backstage/catalog-model' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@backstage/catalog-model';
const { entitySchemaValidator } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Node.js v18.17.1

πŸ‘Ÿ Reproduction steps

package.json

{
  "name": "backstage-module-failure",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "node": "node index.js",
    "fix-module": "mv node_modules/@backstage/catalog-model/dist/index.esm.js node_modules/@backstage/catalog-model/dist/index.mjs && sed -i -- \"s/index.esm.js/index.mjs/g\" node_modules/@backstage/catalog-model/package.json"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@backstage/catalog-model": "^1.4.1"
  }
}

index.js

import { entitySchemaValidator } from "@backstage/catalog-model";

console.log("Success!");

Setup
npm i

Fails
npm run node

Passes
npm run fix-module
npm run node

πŸ“ƒ Provide the context for the Bug.

Trying to validate catalog-info files outside of backstage. Eg in a github action.
Other dependencies require our library to be esm.

πŸ–₯️ Your Environment

% npm --version
9.6.7
% node --version
v18.17.1

πŸ‘€ Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

🏒 Have you read the Code of Conduct?

Are you willing to submit PR?

Yes I am willing to submit a PR!

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions