question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] Using Bree with NX monorepo and typescript

See original GitHub issue

I’d like to run Bree as part of a monorepo. Eventually this application should be able to be ran in a Docker container. I’ve setup Bree as an application (apps/application), in which I would like to load a library (libs/lib). In the root of the application (index.ts) this library is available, but it isn’t found in jobs/job.ts. My guess is that it has something to do with the workers that Bree uses, but I haven’t found anything related to my problem.

The error I get when I run nx serve application is:

Worker for job "job" had an error {
  err: [worker eval]:2
      console.log((0, lib_1.lib)());
                      ^
  
  ReferenceError: lib_1 is not defined

I’ve setup a demo repository, but I’ll also post some relevant files here:

app/apps/project.json:

{
  "root": "apps/application",
  "sourceRoot": "apps/application/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nrwl/js:tsc",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/apps/application",
        "main": "apps/application/src/index.ts",
        "tsConfig": "apps/application/tsconfig.app.json"
      },
      "dependsOn": [
        {
          "target": "build",
          "projects": "dependencies"
        }
      ]
    },
    "serve": {
      "executor": "@nrwl/js:node",
      "options": {
        "buildTarget": "application:build"
      },
      "dependsOn": [
        {
          "target": "build",
          "projects": "self"
        }
      ]
    },
    "lint": {
      "executor": "@nrwl/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["apps/application/**/*.ts"]
      }
    },
    "test": {
      "executor": "@nrwl/jest:jest",
      "outputs": ["coverage/apps/application"],
      "options": {
        "jestConfig": "apps/application/jest.config.js",
        "passWithNoTests": true
      }
    }
  },
  "tags": []
}

app/application/src/index.ts

import path from 'path';
import Bree from 'bree';

import { lib } from '@nx-bree-typescript/lib';

console.log(lib); // <--- works

const bree = new Bree({
  root: path.join(__dirname, 'jobs'),
  jobs: ['job'],
});

bree.start();

apps/application/jobs/job.ts

import { lib } from '@nx-bree-typescript/lib';

console.log(lib); // doesn't work

I also posted this question on StackOverflow.

Checklist

  • I have read the documentation.
  • I have tried @breejs/ts-worker, but I get the same error

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
shadowgate15commented, Feb 21, 2022

So I have figured out the problem. It is an issue with NX. Basically NX hijacks Module._load to handle the require mappings. It does this by forking and actually running a file to grab the module that overwrites the Module._load then requiring the file that should be run. This is never passed on to the Worker since it is a new process. I will make an issue with NX and then post the link here so we can track it.

As a work around:

    {
      name: 'job',
      path: path.join(
        process.cwd(),
        'node_modules',
        '@nrwl/js/src/executors/node/node-with-require-overrides.js'
      ),
      worker: {
        env: {
          ...process.env,
          NX_FILE_TO_RUN: path.join(__dirname, 'jobs/job.js'),
        },
      },
    }

Jobs can be setup like this and they work. Basically, it recreates the code NX is doing, definitely not pretty. Hopefully, NX will come up with a solution or we can create a plugin for this, eventually.

0reactions
shadowgate15commented, Feb 23, 2022

That fix is just for the js:node executor. You won’t be able to import them into index.ts and use them the way you did because of the way worker_threads. It is a hard limitation of node.

You should be able to specify the files that are bundled in the NX configs somewhere. which should resolve the problem. There also maybe a better way to define the path for webpack. You could also copy the code from that file the path is pointed to into your code base and use that file. Also take a look at how we define the worker in ts-worker, which could make it easier for you to setup the jobs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Bree with NX monorepo and typescript - Stack Overflow
I'd like to run Bree as part of a monorepo. Eventually this application should be able to be ran in a Docker container....
Read more >
Using Bree with Nx Monorepo And Typescript - ADocLib
I'd like to run Bree as part of a monorepo.Eventually this application should be able to be ran in a Docker container.I've setup...
Read more >
Question: using typescript template in an nx monorepo #40
What I've noticed is if I point serverless webpack at tsconfig.app.json , but there is a tsconfig.json file in the same directory I...
Read more >
Pnpm and Nx monorepo. Part 1 | Javier Brea
Why a Node.js monorepo? Those who have worked maintaining many dependent packages in different repositories would answer to this question ...
Read more >
How to Build a Monorepo with Nx, Next.js and TypeScript
We'll discuss the advantages of using the Nx development tools for managing a monorepo, and learn how to use those tools to build...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found