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?