Search code examples
typescriptnpmnestjs

Nest.js and Angular.js monorepo: trying to share library folder


I am trying to share models and functions between two typescript projects (nest.js and angular.js). I just created boilerplate nest.js and angular.js projects in api and ui respectively and a folder called lib containing one typescript file. Angular doesn't have a problem importing typescript files from an upper folder. But nest.js fails with this error:

➜  api git:(master) npm run start

> [email protected] start
> nest start

Error: Cannot find module '/home/amir/workspace/nest-angular-monorepo/api/dist/main'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.Module._load (node:internal/modules/cjs/loader:804:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
npm ERR! Lifecycle script `start` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: [email protected] 
npm ERR!   at location: /home/amir/workspace/nest-angular-monorepo/api 
➜  api git:(master) 

Repository


Solution

  • Here I created a diff, to apply copy it into a file patch.diff and apply git apply patch.diff:

    diff --git a/api/package.json b/api/package.json
    index cc378ed..6e1db4e 100644
    --- a/api/package.json
    +++ b/api/package.json
    @@ -1,5 +1,5 @@
     {
    -  "name": "api",
    +  "name": "@root/api",
       "version": "0.0.1",
       "description": "",
       "author": "",
    @@ -24,7 +24,8 @@
         "@nestjs/core": "^9.0.0",
         "@nestjs/platform-express": "^9.0.0",
         "reflect-metadata": "^0.1.13",
    -    "rxjs": "^7.2.0"
    +    "rxjs": "^7.2.0",
    +    "@root/shared": "*"
       },
       "devDependencies": {
         "@nestjs/cli": "^9.0.0",
    diff --git a/api/src/main.ts b/api/src/main.ts
    index d002cd5..ac2dfb3 100644
    --- a/api/src/main.ts
    +++ b/api/src/main.ts
    @@ -1,6 +1,6 @@
     import { NestFactory } from '@nestjs/core';
     import { AppModule } from './app.module';
    -import { User } from '../../lib/model';
    +import { User } from '@root/shared/model';
     
     const usr: User = { name: 'mair' };
     console.log(usr);
    diff --git a/lib/package.json b/lib/package.json
    new file mode 100644
    index 0000000..e777951
    --- /dev/null
    +++ b/lib/package.json
    @@ -0,0 +1,3 @@
    +{
    +  "name": "@root/shared"
    +}
    diff --git a/package-lock.json b/package-lock.json
    index b78f779..e0f10e9 100644
    --- a/package-lock.json
    +++ b/package-lock.json
    @@ -15,12 +15,14 @@
           ]
         },
         "api": {
    +      "name": "@root/api",
           "version": "0.0.1",
           "license": "UNLICENSED",
           "dependencies": {
             "@nestjs/common": "^9.0.0",
             "@nestjs/core": "^9.0.0",
             "@nestjs/platform-express": "^9.0.0",
    +        "@root/shared": "*",
             "reflect-metadata": "^0.1.13",
             "rxjs": "^7.2.0"
           },
    @@ -7317,19 +7319,22 @@
           }
         },
         "lib": {
    -      "version": "1.0.0",
    -      "extraneous": true,
    -      "license": "ISC"
    +      "name": "@root/shared"
         },
    -    "node_modules/api": {
    +    "node_modules/@root/api": {
           "resolved": "api",
           "link": true
         },
    -    "node_modules/ui": {
    +    "node_modules/@root/shared": {
    +      "resolved": "lib",
    +      "link": true
    +    },
    +    "node_modules/@root/ui": {
           "resolved": "ui",
           "link": true
         },
         "ui": {
    +      "name": "@root/ui",
           "version": "0.0.0",
           "dependencies": {
             "@angular/animations": "^15.2.0",
    @@ -7340,6 +7345,7 @@
             "@angular/platform-browser": "^15.2.0",
             "@angular/platform-browser-dynamic": "^15.2.0",
             "@angular/router": "^15.2.0",
    +        "@root/shared": "*",
             "rxjs": "~7.8.0",
             "tslib": "^2.3.0",
             "zone.js": "~0.12.0"
    diff --git a/ui/package.json b/ui/package.json
    index 67d64b8..f227b68 100644
    --- a/ui/package.json
    +++ b/ui/package.json
    @@ -1,5 +1,5 @@
     {
    -  "name": "ui",
    +  "name": "@root/ui",
       "version": "0.0.0",
       "scripts": {
         "ng": "ng",
    @@ -20,7 +20,8 @@
         "@angular/router": "^15.2.0",
         "rxjs": "~7.8.0",
         "tslib": "^2.3.0",
    -    "zone.js": "~0.12.0"
    +    "zone.js": "~0.12.0",
    +    "@root/shared": "*"
       },
       "devDependencies": {
         "@angular-devkit/build-angular": "^15.2.7",
    diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts
    index c276b9a..0fe97c2 100644
    --- a/ui/src/app/app.component.ts
    +++ b/ui/src/app/app.component.ts
    @@ -1,5 +1,5 @@
     import { Component } from '@angular/core';
    -import {User} from "../../../lib/model";
    +import { User } from '@root/shared/model';
     
     @Component({
       selector: 'app-root',
    

    Build result

    You can rename the package names however you like I named them @root/ui, @root/shared and @root/api

    Edit:

    If you look under node_modules/@root/ you will see that npm only creates links to your packages.

    node_modules/@root