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

Node App with ESBuild + JS Library "Cannot find module". #18985

Closed
1 of 4 tasks
chmoder opened this issue Sep 3, 2023 · 10 comments
Closed
1 of 4 tasks

Node App with ESBuild + JS Library "Cannot find module". #18985

chmoder opened this issue Sep 3, 2023 · 10 comments
Labels
outdated scope: bundlers Issues related to webpack, rollup stale type: bug

Comments

@chmoder
Copy link

chmoder commented Sep 3, 2023

Current Behavior

When I create a brand new project with a node app and library. Then import the library into the app and run :serve , it errors with cannot find module.

Expected Behavior

It builds affected libs and has them available for the app.

GitHub Repo

No response

Steps to Reproduce

  1. npx create-nx-workspace@latest test-workspace --preset node-monorepo --framework express --nxCloud false
  2. npx nx generate @nx/node:application --name=test-app --framework=express
  3. npx nx generate @nx/js:library --name=test-lib --unitTestRunner=jest --importPath=@test-app/test-lib --projectNameAndRootFormat=as-provided --bundler esbuild
  4. npx nx run test-app:serve (server will start)
  5. Add test-lib to app main
import { testLib } from '@test-app/test-lib';
import express from 'express';

const host = process.env.HOST ?? 'localhost';
const port = process.env.PORT ? Number(process.env.PORT) : 3000;

const app = express();

app.get('/', (req, res) => {
  testLib();
  res.send({ message: 'Hello API' });
});

app.listen(port, host, () => {
  console.log(`[ ready ] http://${host}:${port}`);
});
  1. npx nx run test-app:serve

Nx Report

Node   : 20.5.1
   OS     : darwin-arm64
   npm    : 10.0.0
   
   nx                 : 16.7.4
   @nx/js             : 16.7.4
   @nx/jest           : 16.7.4
   @nx/linter         : 16.7.4
   @nx/workspace      : 16.7.4
   @nx/devkit         : 16.7.4
   @nx/esbuild        : 16.7.4
   @nx/eslint-plugin  : 16.7.4
   @nx/node           : 16.7.4
   @nrwl/tao          : 16.7.4
   typescript         : 5.1.6

Failure Logs

Error: Cannot find module '/Users/tcross/temp/test-workspace/dist/test-lib'
Require stack:
- /Users/tcross/temp/test-workspace/dist/apps/test-app/apps/test-app/src/main.js
- /Users/tcross/temp/test-workspace/dist/apps/test-app/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
    at Function.Module._resolveFilename (/Users/tcross/temp/test-workspace/tmp/test-app/main-with-require-overrides.js:41:36)
    at Function.Module._load (node:internal/modules/cjs/loader:901:27)
    at Function.Module._load (/Users/tcross/temp/test-workspace/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js:15:31)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/Users/tcross/temp/test-workspace/apps/test-app/src/main.ts:1:25)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

Work Arounds:

  1. Build the library before running serve
  2. Set "bundle": true, in the apps project.json > build > options
  3. Use webpack instead of ESBuild for the app bundler
@kurt-west
Copy link

I had a similar error Cannot find module './utils/index.js' and stack trace on my build events. It started after doing a general deps (nx and minor packages) upgrade. Rolling back, I found the upgrade of @swc-node/[email protected] to @swc-node/[email protected] caused the error.

My project has a custom executor that generates/updates index barrel files on each build, when I disable this executor the error goes away, so it looks to be related to executors.

@chmoder
Copy link
Author

chmoder commented Sep 4, 2023

Sorry, but I am new to nx and builds, so I am not sure what the "best" way to build the project is. I was learning how NX works and picked the defaults and struggled to get it to run and learned why it isn't working and wanted to let the team know. I may be able to help make a PR with a little direction.

npx nx run test-app:serve

test-workspace % tree -h dist 
[  96]  dist
└── [  96]  apps
    └── [ 256]  test-app
        ├── [  96]  apps
        │   └── [  96]  test-app
        │       └── [ 128]  src
        │           ├── [1.6K]  main.js
        │           └── [ 879]  main.js.map
        ├── [1.4K]  main.js
        ├── [2.9K]  main.js.map
        ├── [ 22K]  package-lock.json
        ├── [ 141]  package.json
        └── [  96]  test-lib  <------------------------ I don't think the library should be in the app.
            └── [ 160]  src
                ├── [1.1K]  index.js
                ├── [ 207]  index.js.map
                └── [ 128]  lib
                    ├── [1.1K]  test-lib.js
                    └── [ 279]  test-lib.js.map

9 directories, 10 files

npx nx run test-lib:build

test-workspace % tree -h dist             
[ 128]  dist
├── [  96]  apps
│   └── [ 256]  test-app
│       ├── [  96]  apps
│       │   └── [  96]  test-app
│       │       └── [ 128]  src
│       │           ├── [1.6K]  main.js
│       │           └── [ 879]  main.js.map
│       ├── [1.4K]  main.js
│       ├── [2.9K]  main.js.map
│       ├── [ 22K]  package-lock.json
│       ├── [ 141]  package.json
│       └── [  96]  test-lib
│           └── [ 160]  src
│               ├── [1.1K]  index.js
│               ├── [ 207]  index.js.map
│               └── [ 128]  lib
│                   ├── [1.1K]  test-lib.js
│                   └── [ 279]  test-lib.js.map
└── [ 192]  test-lib <--------------------- In my project it would be in libs/ but this works and shows the issue/workaround.
    ├── [ 230]  README.md
    ├── [1.1K]  index.cjs
    ├── [ 224]  package-lock.json
    └── [ 126]  package.json

10 directories, 14 files

@kurt-west
Copy link

I was able to fix my issue by getting @nx/plugin:executor to use ts-node vs. swc-node.

How to switch the executor to ts-node:

  1. Verify you have ts-node installed; if not install it.
  2. Create an .env in your root project folder with NX_PREFER_TS_NODE=true
  3. Run the command that was failing.

@chmoder
If test-lib is using @nx/js:swc, you could try switch to @nx/js:tsc and see if that resolves your problem.

@chmoder
Copy link
Author

chmoder commented Sep 4, 2023

Thanks for the suggestions! Using the .env with prefer ts node didn't change the nx serve result.

Here is the build section from test-lib:

 "build": {
      "executor": "@nx/esbuild:esbuild",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/test-lib",
        "main": "test-lib/src/index.ts",
        "tsConfig": "test-lib/tsconfig.lib.json",
        "assets": ["test-lib/*.md"],
        "generatePackageJson": true,
        "format": ["cjs"]
      }
    }

It is using @nx/esbuild:esbuild. Using swc or tsc didn't change it either.

And here is the build phase of test-app`.

    "build": {
      "executor": "@nx/esbuild:esbuild",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "platform": "node",
        "outputPath": "dist/apps/test-app",
        "format": ["cjs"],
        "bundle": false,
        "main": "apps/test-app/src/main.ts",
        "tsConfig": "apps/test-app/tsconfig.app.json",
        "assets": ["apps/test-app/src/assets"],
        "generatePackageJson": true,
        "esbuildOptions": {
          "sourcemap": true,
          "outExtension": {
            ".js": ".js"
          }
        }
      },

Source:
test-workspace.zip

@kurt-west
Copy link

kurt-west commented Sep 4, 2023

@chmoder - Your issue looks to be different than mine. With a couple minor changes to the project structure I was able to get the project to build and run.

Updated source:
test-workspace-working.zip

@chmoder
Copy link
Author

chmoder commented Sep 4, 2023

I see the changes you made, and it looks like it works but it is a little tricky. If you do rm -r dist then yarn run start you should see something like this:

Error: Cannot find module '/Users/tcross/Downloads/test-workspace-working/dist/test-lib'
Require stack:
- /Users/tcross/Downloads/test-workspace-working/dist/apps/test-app/apps/test-app/src/main.js
- /Users/tcross/Downloads/test-workspace-working/dist/apps/test-app/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
    at Function.Module._resolveFilename (/Users/tcross/Downloads/test-workspace-working/tmp/test-app/main-with-require-overrides.js:41:36)
    at Function.Module._load (node:internal/modules/cjs/loader:901:27)
    at Function.Module._load (/Users/tcross/Downloads/test-workspace-working/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js:15:31)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/Users/tcross/Downloads/test-workspace-working/apps/test-app/src/main.ts:1:25)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)

If you do yarn run build, then yarn run start, then the application will load. This is the behavior I have been experiencing too. This may be an ESBuild / configuration issue and the only reason I bring it up is because these are the defaults chosen by the generator scripts (similar to the node server tutorial). I appreciate all your help. I will keep using the workarounds till I see more feedback on this issue or others like it.

@wszydlak
Copy link

wszydlak commented Sep 6, 2023

I had a similar error Cannot find module './utils/index.js' and stack trace on my build events. It started after doing a general deps (nx and minor packages) upgrade. Rolling back, I found the upgrade of @swc-node/[email protected] to @swc-node/[email protected] caused the error.

My project has a custom executor that generates/updates index barrel files on each build, when I disable this executor the error goes away, so it looks to be related to executors.

I reported it in swc-project/swc-node#729, not sure if this should be fixed on their side or nx

@kurt-west
Copy link

I had a similar error Cannot find module './utils/index.js' and stack trace on my build events. It started after doing a general deps (nx and minor packages) upgrade. Rolling back, I found the upgrade of @swc-node/[email protected] to @swc-node/[email protected] caused the error.
My project has a custom executor that generates/updates index barrel files on each build, when I disable this executor the error goes away, so it looks to be related to executors.

I reported it in swc-project/swc-node#729, not sure if this should be fixed on their side or nx

Thank you - I confirmed the issue was introduced with the addition of a default baseUrl inside @swc-node/register. Added some notes to the SWC issues.

Copy link

github-actions bot commented Mar 7, 2024

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs.
If we missed this issue please reply to keep it active.
Thanks for being a part of the Nx community! 🙏

Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: bundlers Issues related to webpack, rollup stale type: bug
Projects
None yet
Development

No branches or pull requests

4 participants