Hello everyone, I have a problem and I can't solve...
# typescript
e
Hello everyone, I have a problem and I can't solve it. I want to implement path aliases in typescript. when i run:
pulumi up -y -s dev
my error
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Array
this is my
tsconfig.ts
Copy code
{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@application/*": [
        "application/*"
      ],
      "@config/*": [
        "config/*"
      ],
      "@domain/*": [
        "domain/*"
      ],
      "@api-route": [
        "api-route.ts"
      ],
      "@interfaces/*": [
        "interfaces/*"
      ]
    },
    "strict": true,
    "outDir": "bin",
    "target": "ESNEXT",
    "lib": [
      "ESNEXT"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "experimentalDecorators": true,
    "pretty": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [
      "@types/node",
      "node"
    ]
  },
  "files": [
    "src/handler.ts"
  ],
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}
my package.json
Copy code
{
  "name": "api-notifications",
  "scripts": {
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "prettify": "prettier . --write"
  },
  "main": "src/handler.ts",
  "devDependencies": {
    "@types/aws-sdk": "^2.7.0",
    "@types/dotenv": "^8.2.0",
    "@types/node": "^10.17.60",
    "typescript": "^4.2.4",
    "tslint": "^6.1.3",
    "tslint-config-airbnb": "^5.11.2"
  },
  "_moduleAliases": {
    "@application": [
      "bin/application"
    ],
    "@config": [
      "bin/config"
    ],
    "@domain": [
      "bin/domain"
    ],
    "@interfaces": [
      "bin/interfaces"
    ]
  },
  "dependencies": {
    "@pulumi/aws": "^4.0.0",
    "@pulumi/awsx": "^0.30.0",
    "@pulumi/pulumi": "^3.0.0",
    "@types/aws-lambda": "^8.10.76",
    "axios": "^0.21.1",
    "dotenv": "^9.0.2",
    "module-alias": "^2.2.2"
  }
}
Any idea or example to add the path aliases? for example: instead of this
import { CustomEventService } from "../../../application/services/custom-event/custom-event.service";
use this:
import { CustomEventService } from "@application/services/custom-event/custom-event.service";
in my
./src/handler.ts
I add
Copy code
import "module-alias/register";
l
This is of interest to me too. I tried this, and I got a different error (Cannot find module '@mypath'), though it works in VSCode. I tried turning off the built-in typescript (Pulumi.yaml, runtime options typescript: false) and compiling myself, but no change.
Also couldn't get it working with Mocha, but that's expected, since Mocha spec files are compiled using
"module": "commonjs"
. The Pulumi files that the spec files import work fine with aliases.
That is, I have
import { x } from "@mymodule";
in my .ts files, but I need
import { x } from "../../../../src/resources/mymodule";
in my .spec.ts files.
e
my solution is:
Copy code
"_moduleAliases": {
    "@application": "src/application",
    "@config": "src/config",
    "@domain": "src/domain",
    "@interfaces": "src/interfaces",
    "@api-route": "src/api-route"
  },
l
Does that require the module-alias module? Instead of the built-in path aliases?
e
I implemented it using the
"module-alias": "^2.2.2"
l
Good stuff. I moved the "path" section out of "compilerOptions" to the top level, renamed it to "_moduleAliases", removed the arrays (leaving just the paths), and added "module-alias/register" to my .mocharc.json require property. No joy 😞 "Cannot find module" errors...
@echoing-zebra-28421 How do you call
module-alias/register
? Do you use
require("module-alias")
in all your projects? I'm wondering if there's a way to do it using package.json or tsconfig.json.
e
@little-cartoon-10569 steps to follow:
npm i --save module-alias
in the
tsconfig.json
you define your paths: In my case
Copy code
"compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@application/*": ["application/*"],
      "@config/*": ["config/*"],
      "@domain/*": ["domain/*"],
      "@api-route": ["api-route.ts"],
      "@interfaces/*": ["interfaces/*"]
    },
in the
package.json
you define the
_moduleAliases
in my case
Copy code
"_moduleAliases": {
    "@application": "src/application",
    "@config": "src/config",
    "@domain": "src/domain",
    "@interfaces": "src/interfaces",
    "@api-route": "src/api-route"
  },
now in the
index.ts
(in my case is
handler.ts
) you add
import "module-alias/register";
l
Hmm, I did that, still complained...
I've just read this the "paths and baseUrl" section of https://www.npmjs.com/package/ts-node so I'm going to try that now...
e
the baseURL is usually at the level of tsconfig.ts
Copy code
my-project
    -- src
        -- index.ts
        -- folder1
        -- folder2
        ...
    -- tsconfig.ts
    ...
l
I see that your tsconfig.ts refers to ./src, but in your folder structure, that would be ../src...?
c
I have a
entry.ts
file which my package.json points to. it has
Copy code
const path = require('path')
const tsConfig = require(__dirname + '/../../../tsconfig.base.json')
const tsConfigPaths = require('tsconfig-paths')
const args = {
    baseUrl: path.resolve(__dirname + '/../../..'),
    paths: tsConfig.compilerOptions.paths,
}
tsConfigPaths.register(args)

module.exports = require('./index')
I am using NX
Which is why the tsconfig is a few folders up
l
I have a roll-my-own monorepo. Might look into NX. Though I see all those `require()`s and think.. esnext, es2020, ...
c
index.ts and down is all esmodules
it’s just this init file so I can bootstrap it
May be a better way, but it works
Working towards being able to do
nx up --affected