1

I have the following script located in my project root:

// ./root-dir.ts
import * as url from "url";
export const rootDir = url.fileURLToPath(new URL(".", import.meta.url));

I have my tsconfig.json set up as follows:

{
    "extends": "astro/tsconfigs/base",
    "compilerOptions": {
        "baseUrl": ".",
        "outDir": "./tmp",
        "plugins": [
            {
                "name": "@astrojs/ts-plugin"
            }
        ],
        "jsx": "preserve",
        "paths": {
            "@*": [
                "./src/*"
            ],
            "@root/*": [
                "./*"
            ],
            "@email/*": [
                "./email/*"
            ],
            "/opt/nodejs/*": [
                "./lambda/layer/newsletter-helper/nodejs/*",
                "./lambda/layer/sdk-wrapper/nodejs/*"
            ]
        },
    },
    "ts-node": {
        "esm": true,
        "require": [
            "tsconfig-paths/register"
        ]
    },
    "exclude": [
        "node_modules",
        "*/node_modules"
    ]
}

and I have the following script that imports the root dir via the root alias

// ./lambda/zip-layers.ts
import fs from "fs";
import path from "path";
import { rootDir } from "@~/root-dir";
import * as child_process from "child_process";

const zipPath = path.resolve(rootDir, "./lambda/zip/layer")
const layersPath = path.resolve(rootDir, "./lambda/layer");

const getChildDirectories = (path) =>
    fs.readdirSync(path, { withFileTypes: true })
        .filter(dirent => dirent.isDirectory())
        .map(dirent => dirent.name);

function zipLayers() {
    for (const layer of getChildDirectories(layersPath)) {
        const layerPath = `${layersPath}/${layer}`;
        const zippedPath = `${zipPath}/${layer}.zip`; 
        const command = "zip -r " + zippedPath + " -r " + layerPath;
    
        child_process.exec(command);
    }
}

zipLayers();

I want to run the zip layers file from the command line. I thought with the tsconfig.json settings I have (notice with tsconfig-paths installed as a dev dependency) that pnpx ts-node ./lambda/zip-layers.ts would have been sufficient to run this code but I keep getting the following module not found error:

  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
   CustomError: Cannot find package '@~/root-dir' ...
    at packageResolve 

Anyone got any ideas as to how I can run this from the command line whilst maintaining tsconfig.json alias paths?

feraleyebrows
  • 155
  • 1
  • 5

1 Answers1

0

Just spent literally hours trying to get this to work. In the end, I ended up ditching ts-node entirely and installing tsx which just worked straight out of the box.

Apparently, esm support for ts alias paths is coming to ts-node eventually, although the PR kind of looks dead so who knows:

https://github.com/TypeStrong/ts-node/pull/1585

In the mean time, if you really want to stick with ts-node you may find the following useful:

https://www.npmjs.com/package/@bleed-believer/path-alias

https://github.com/TypeStrong/ts-node/issues/1007

https://github.com/TypeStrong/ts-node/discussions/1450#discussioncomment-1806115

Edit: I am currently unable to get tsx to work with my "/opt/nodejs/*" alias but it does work fine for the other aliases starting with @.

feraleyebrows
  • 155
  • 1
  • 5