This message was deleted.
# typescript
s
This message was deleted.
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