1

I have defined a custom path as answered in this thread.

"baseUrl": ".",
"paths": {
  "@library/*": [
    "./src/myFolder/*"
  ],
}

From this module, I export an Enum.

export enum ENUM {
   ONE = "one"
}

When trying to use it with the custom path, I get

Module not found: Can't resolve @path/I/imported/from in /path/to/src

No problem if imported directly with '../relative/path'

Here is a link to minimal project showing the problem. (Edit: I changed the code to stop using create-react-app, an error occur when the localhost:3400 is visited)

I first got this problem in a node project with express.

I guess that the problem comes from the fact that typescript types are not included in the compiled js and thus the value present in the enum is not present. It still seems strange that there is no warning about the fact that this "one" value might not be included.

My question is what would be the best approach to use custom path and exported enums ?

lblenner
  • 372
  • 2
  • 14

2 Answers2

1

Ohk, I read the edited question and checkout your project. Below are changes I made:

I used tsconfig-paths module: This will allow to properly build the app using root import alias and map to physical paths in the filesystem.

UPDATED tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "preserveConstEnums": true,  //<----change
    "module": "commonjs",
    "outDir": "dist",
    "sourceMap": true,
    "baseUrl": "./src", //<----change
    "esModuleInterop": true,
    "paths": {
      "@library/*": [
        "myFolder/*"  //<----change
      ],
    }
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    ".vscode"
  ]
}

UPDATED package.json

{
  "name": "test2",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.ts",
  "scripts": {
    "start:dev": "nodemon",
    "start": "node --inspect=5858 -r tsconfig-paths/register -r ts-node/register ./src/server.ts",  //<----change
    "build": "tsc"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "tsconfig-paths": "^3.9.0" //<----change
  },
  "devDependencies": {
    "@types/express": "^4.17.8",
    "@types/node": "^14.11.1",
    "concurrently": "^5.3.0",
    "nodemon": "^2.0.4",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.2"
  },
  "nodemonConfig": {
    "ignore": [
      "**/*.test.ts",
      "**/*.spec.ts",
      ".git",
      "node_modules"
    ],
    "watch": [
      "src"
    ],
    "exec": "npm start",
    "ext": "ts"
  }
}

and an additional file named as tsconfig-paths-bootstrap.js (adjacent to tsconfig.json), with below content:

const tsConfig = require('./tsconfig.json');
const tsConfigPaths = require('tsconfig-paths');
const baseUrl = './build';
tsConfigPaths.register({
    baseUrl,
    paths: tsConfig.compilerOptions.paths,
});

And Everything WORKED Successfully! Hope it helps!

enter image description here

Dolly
  • 2,213
  • 16
  • 38
  • Thanks for the response but the problem does not come from cra, I used cra to quickly create a project but the problem occurred while working with firebase functions. I edited the github project to show the problem using express. – lblenner Sep 17 '20 at 20:24
  • ohk got it and I change the answer as per edited question. Hope it helps!!! – Dolly Sep 19 '20 at 04:53
  • Thanks, seems like the preserveConstEnums option is not needed to make it work though. – lblenner Sep 19 '20 at 16:12
0

This is because create-react-app doesn't support path aliasing and probably won't for the near future.

Please see the comment and the thread

  • Thanks for the response but the problem does not come from cra, I used cra to quickly create a project but the problem occurred while working with firebase functions. I edited the github project to show the problem using express. – lblenner Sep 17 '20 at 20:24