Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create path alias for file(s) outside of project

I am working on a project that is split up between multiple angular projects. I then have some services that are used in each project. Is there a way for me to load these in my project? I am getting errors when I try to do so.

/root
  /project-1
    /angular.json
    /tsconfig.json
  /project-2
    /angular.json
    /tsconfig.json
  /utilities
    /src
      /auth.service.ts
      /s3.service.ts
      /lambda.service.ts

I have this in the root of the the angular projects tsconfig.json file (project-1, project-2):

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@utilities/*": [
        "../utilities/src/*"
      ]
    }
  }
}

when I run npm start in project-1, I get the following error(s):

Error: ../utilities/node_modules/buffer-equal-constant-time/index.js
Module not found: Error: Can't resolve 'buffer' in 'C:\projects\utilities\node_modules\buffer-equal-constant-time'

// Same error repeated for different modules...       

In my utilities/package.json

{
  "dependencies": {
    "@angular/core": "^11.0.5",
    "@aws-sdk/client-cognito-identity": "^3.0.0",
    "@aws-sdk/client-lambda": "^3.0.0",
    "@aws-sdk/credential-provider-cognito-identity": "^3.0.0",
    "jsonwebtoken": "^8.5.1",
    "rxjs": "^6.6.3",
    "tslib": "^2.0.0"
  },
}
like image 951
Get Off My Lawn Avatar asked Nov 06 '22 03:11

Get Off My Lawn


1 Answers

I have figured out one way to do this. In the utilities folder make it a link npm link. Then in the project-1 folder link to the project npm link utilities.

I then had to add some paths and includes in the tsconfig.json for references:

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@utilities/*": [
        "node_modules/utilities/src/*"
      ]
    }
  },
  "include": [
    "node_modules/utilities/src/**/*.ts"
  ]
}
like image 107
Get Off My Lawn Avatar answered Nov 12 '22 12:11

Get Off My Lawn