nrwl / nx

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

@nrwl/node:node executor does not support importing ESM libs #11335

Open owen26 opened 2 years ago

owen26 commented 2 years ago

Current Behavior

Error [ERR_REQUIRE_ESM]: require() of ES Module from not supported.

Expected Behavior

Npm libs bundled with ESM only module should be supported.

Steps to Reproduce

Failure Logs

Error [ERR_REQUIRE_ESM]: require() of ES Module from not supported.

Environment

Node : 16.15.0 OS : darwin arm64 yarn : 1.22.19

nx : 14.1.1 @nrwl/angular : Not Found @nrwl/cypress : 14.1.1 @nrwl/detox : Not Found @nrwl/devkit : 14.1.1 @nrwl/eslint-plugin-nx : 14.1.1 @nrwl/express : 14.1.9 @nrwl/jest : 14.1.1 @nrwl/js : 14.1.1 @nrwl/linter : 14.1.1 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : 14.1.9 @nrwl/nx-cloud : Not Found @nrwl/nx-plugin : Not Found @nrwl/react : 14.1.1 @nrwl/react-native : Not Found @nrwl/schematics : Not Found @nrwl/storybook : 14.1.1 @nrwl/web : 14.1.1 @nrwl/workspace : 14.1.1 typescript : 4.6.4 rxjs : 6.6.7

Possibly related issue: https://github.com/nrwl/nx/issues/10296

danielsharvey commented 5 months ago

In case it is useful, I've published a Node executor including ESM module resolution for buildable libraries within Nx workspaces.

https://www.npmjs.com/package/@harves/nx-node-esm-plugin

niallconaghan commented 5 months ago

Hi, I have the same issue - thanks for sharing your executor @danielsharvey. I tried to implement it by updating my project.json:

    "serve": {
        "executor": "@harves/nx-node-esm-plugin:node",
        "defaultConfiguration": "development",
        "options": {
            "buildTarget": "authentication:build"
        },
        "configurations": {
            "development": {
                "buildTarget": "authentication:build:development"
            },
            "production": {
                "buildTarget": "authentication:build:production"
            }
        }
    },

Sadly I'm still getting the same error, perhaps I am missing something?

nx: 17.3.2 node v21.3.0

If its any use I believe nodemailer is causing the issue. Any additional insight is appreciated. Thanks

danielsharvey commented 5 months ago

@niallconaghan Can you say more about the failure / error you are seeing? nodemail publishes a CommonJS library from my look - how are you require'ing that into your library/app?

g3-tin commented 4 months ago

Hope this will help someone :)

    "serve": {
      "executor": "nx:run-commands",
      "dependsOn": ["build"],
      "defaultConfiguration": "development",
      "options": {
        "watch": true,
        "command": "concurrently -n \"runner,builder-watcher\" \"pnpm exec tsx --watch dist/apps/g3-worker/main.js\" \"pnpm exec nx watch --projects=g3-worker -d -- pnpm exec nx build g3-worker\""
      },
      "configurations": {
        "development": {},
        "production": {}
      }
    },
itizarsa commented 2 weeks ago

@danielsharvey Your library works well for serve. How do we make it work for build?

I built your sample app using nx build test-app

Then ran node dist/apps/test-app/src/index.js

Getting the below error

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

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/arshath/Dev/nx/node_modules/nx/test-lib' imported from /Users/arshath/Dev/nx/dist/apps/test-app/src/lib/test-app.js
    at finalizeResolution (node:internal/modules/esm/resolve:265:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
    at defaultResolve (node:internal/modules/esm/resolve:1169:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:510:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:96:40)
    at link (node:internal/modules/esm/module_job:95:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///Users/arshath/Dev/nx/node_modules/nx/test-lib'
}

Node.js v20.18.0