nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.81k stars 2.38k forks source link

CommonJS executor not running in commonjs #26617

Open intellix opened 5 months ago

intellix commented 5 months ago

Current Behavior

We're creating an executor that can read a graphql schema and output a .graphql file for generating types later. Our project is commonjs because we have bi-directional circular issues: https://github.com/nrwl/nx/issues/2975#issuecomment-782069044

When we run the executor (commonjs) against a library (commonjs) we're getting an error as though the commonjs module setting is completely ignored:

ReferenceError: Cannot access 'Tag' before initialization

I'm assuming that it's not running in a commonjs environment:

Command:

npx nx run db:extract-schema --verbose

Expected Behavior

It should run the executor in commonjs mode.

GitHub Repo

https://github.com/intellix/nx-commonjs-executor

Steps to Reproduce

  1. npm install
  2. npx nx run db:extract-schema

Nx Report

Node   : 20.13.1
OS     : darwin-arm64
npm    : 10.5.2

nx                 : 19.3.0
@nx/js             : 19.3.0
@nx/jest           : 19.3.0
@nx/linter         : 19.3.0
@nx/eslint         : 19.3.0
@nx/workspace      : 19.3.0
@nx/devkit         : 19.3.0
@nx/esbuild        : 19.3.0
@nx/eslint-plugin  : 19.3.0
@nx/node           : 19.3.0
@nx/plugin         : 19.3.0
@nrwl/tao          : 19.3.0
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/jest/plugin

Failure Logs

ReferenceError: Cannot access 'Tag' before initialization
    at Object.get (/Proejcts/org/libs/db/src/lib/tag.model.ts:5:9)
    at Object.<anonymous> (/Proejcts/org/libs/db/src/lib/entity-tag.model.ts:42:52)
    at Module._compile (node:internal/modules/cjs/loader:1358:14)
    at Module._compile (/Proejcts/org/node_modules/pirates/lib/index.js:117:24)
    at Module._compile (/Proejcts/org/node_modules/pirates/lib/index.js:117:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at newLoader (/Proejcts/org/node_modules/pirates/lib/index.js:121:7)
    at Object.newLoader [as .ts] (/Proejcts/org/node_modules/pirates/lib/index.js:121:7)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1024:12)

Package Manager Version

No response

Operating System

Additional Information

If I try to run this code (from Chat GPT) it says that I'm running in a CommonJS environment, but I'm not sure (I have to remove the ESM check cause it doesn't compile but it still logs CommonJS):

Screenshot 2024-06-26 at 21 51 54

Another confusing thing, is that I can't get it to fail with a serve/build when I remove commonjs as the module (everywhere) to try and replicate the issue that used to exist (https://github.com/nrwl/nx/pull/5129/files):

npx nx run org:serve
npx nx run org:build && node dist/apps/org/main.js
adammfrank commented 3 months ago

@intellix Did you find any workaround? I think I'm also running into this. All imports within my libraries that I import into my executor are failing.

intellix commented 3 months ago

nope haven't found a workaround. Was hoping to move our extract-schema scripts to executors but right now I'm blocked on that so moved onto other work and was awaiting some more information about this