[question] Using Bree with NX monorepo and typescript
See original GitHub issueI’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:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 theModule._load
then requiring the file that should be run. This is never passed on to theWorker
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:
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.
That fix is just for the
js:node
executor. You won’t be able to import them intoindex.ts
and use them the way you did because of the wayworker_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 theworker
ints-worker
, which could make it easier for you to setup the jobs.