ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.75k stars 69 forks source link

Cannot find module when compiling #108

Closed chan-dev closed 4 years ago

chan-dev commented 4 years ago

I have this kind of structure. I omit some of the files

├── server/
│   ├── tsconfig.json
│   └── package.json
└── shared/
    ├── helpers/validators.ts
    ├── constants/tags.ts
    └── models/bookmark.ts

Here's my tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@models/*": ["../shared/models/*"],
      "@constants/*": ["../shared/constants/*"],
      "@helpers/*": ["../shared/helpers/*"]
    },
    "target": "es2018",
    "module": "commonjs",
    "moduleResolution": "node",
    "rootDir": "../",
    "outDir": "./build",
    "esModuleInterop": true,
    "strict": true,
    "typeRoots": ["node_modules/@types"],
    "sourceMap": false,
    "removeComments": true
  },
  "include": ["**/*", "../shared/**/*"]
}

Here's the package.json with moduleAlias

{
  "name": "pocketlite-server",
  "version": "1.0.0",
  "engines": {
    "node": "14.x",
    "npm": "6.x"
  },
  "description": "",
  "main": "index.js",
  "_moduleAliases": {
    "@models": "build/shared/models",
    "@constants": "build/shared/constants",
    "@helpers": "build/shared/helpers"
  },
  "scripts": {
    "start:prod": "node build/server/index.js",
    "start:staging": "export NODE_ENV=staging && nodemon -r tsconfig-paths/register index.ts",
    "start:dev": "export NODE_ENV=development && nodemon -r tsconfig-paths/register index.ts",
    "build": "tsc -p .",
    "heroku-prebuild": "npm i --only=dev",
    "lint": "eslint . --ext .ts",
    "csrf": "npx http-server -c-1 -d false -o /csrf-test"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/cookie-parser": "^1.4.2",
    "@types/cors": "^2.8.7",
    "@types/express": "^4.17.7",
    "@types/jsdom": "^16.2.3",
    "@types/mongoose": "^5.7.36",
    "@types/mongoose-delete": "^0.5.1",
    "@types/mozilla-readability": "^0.2.0",
    "@types/node": "^14.0.27",
    "@types/passport": "^1.0.4",
    "@types/passport-google-oauth20": "^2.0.3",
    "@types/passport-jwt": "^3.0.3",
    "@types/puppeteer": "^3.0.1",
    "@types/turndown": "^5.0.0",
    "@typescript-eslint/eslint-plugin": "^3.8.0",
    "@typescript-eslint/parser": "^3.8.0",
    "dotenv": "^8.2.0",
    "eslint": "^7.6.0",
    "nodemon": "^2.0.4",
    "ts-node": "^8.10.2",
    "tsconfig-paths": "^3.9.0"
  },
  "dependencies": {
    "@mozilla/readability": "^0.3.0",
    "@types/cookie-session": "^2.0.41",
    "@types/csurf": "^1.9.36",
    "cookie-parser": "^1.4.5",
    "cookie-session": "^1.4.0",
    "cors": "^2.8.5",
    "csurf": "^1.11.0",
    "express": "^4.17.1",
    "isomorphic-dompurify": "^0.6.0",
    "jsdom": "^16.4.0",
    "jsonwebtoken": "^8.5.1",
    "module-alias": "^2.2.2",
    "mongoose": "^5.9.28",
    "mongoose-delete": "^0.5.2",
    "passport": "^0.4.1",
    "passport-google-oauth20": "^2.0.0",
    "passport-jwt": "^4.0.0",
    "puppeteer": "^5.2.1",
    "puppeteer-extra": "^3.1.15",
    "puppeteer-extra-plugin-adblocker": "^2.11.6",
    "puppeteer-extra-plugin-stealth": "^2.6.1",
    "turndown": "^6.0.0",
    "turndown-plugin-gfm": "^1.0.2",
    "typescript": "^4.0.3"
  }
}

Here's the relevant npm scripts

  "scripts": {
    "start:prod": "node build/server/index.js",
    "start:staging": "export NODE_ENV=staging && nodemon -r tsconfig-paths/register index.ts",
    "start:dev": "export NODE_ENV=development && nodemon -r tsconfig-paths/register index.ts",
    "build": "tsc -p .",
  }

When i execute npm run build in docker or heroku. It always returns this kind of errors:

helpers/link-scraper.ts(12,26): error TS2307: Cannot find module '@models/bookmark.model' or its corresponding type declarations.
helpers/validators.ts(1,23): error TS2307: Cannot find module '@constants/patterns' or its corresponding type declarations.
models/bookmark-favorite.ts(2,34): error TS2307: Cannot find module '@models/bookmark-favorite.model' or its corresponding type declarations.
models/bookmark.ts(7,26): error TS2307: Cannot find module '@models/bookmark.model' or its corresponding type declarations.
models/tag.ts(2,21): error TS2307: Cannot find module '@models/tag.model' or its corresponding type declarations.
models/user.ts(2,22): error TS2307: Cannot find module '@models/user.model' or its corresponding type declarations.
routes/bookmarks.ts(4,32): error TS2307: Cannot find module '@constants/tags' or its corresponding type declarations.

Looking at the compiled javascript files, the path mappings are not resolved to their corresponding paths

Here's an snippet of one of the compiled js files

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const mongoose_1 = __importDefault(require("mongoose"));
const tags_1 = require("@constants/tags");
const bookmark_1 = __importDefault(require("../models/bookmark"));
const tag_1 = __importDefault(require("../models/tag"));
const bookmark_favorite_1 = __importDefault(require("../models/bookmark-favorite"));
const link_scraper_1 = __importDefault(require("../helpers/link-scraper"));
const paginate_1 = __importDefault(require("../middlewares/paginate"));
const auth_jwt_1 = __importDefault(require("../middlewares/auth-jwt"));
const error_1 = require("../classes/error");
const validators_1 = require("../helpers/validators");
const router = express_1.default.Router();

As you can see the line require("@constants/tags") is not resolved.

Kehrlann commented 4 years ago

Hey @chan-dev ! Thanks for contributing. So this is a tsc compilation problem as far as I see. It doesn't have much to do with module-alias. It might be that the build script is not run from the correct directory in the heroku buildpack... They do a bunch of stuff which might lead to surprise effects (for example, node_modules is installed somewhere outside your project, and then symlinked into your project dir).

If I were you, I'd try to:

  1. Make sure that "npm run build" actually runs in your server directory in Heroku
  2. Make sure it picks up your tsconfig.json
  3. And make sure that ../shared is actually there

I guess I see what you mean with:

const tags_1 = require("@constants/tags");

However this should work with module-alias though, as long as you run require('module-alias/register') before.


Closing this as it's more tsc + Heroku related.