Skip to content

Absolute path hell with TypeScript / NodeJS #29272

@jimpouwels

Description

@jimpouwels

There's probably multiple questions on this already, but so far I've not been able to tackle this issue for my scenario.

Background:

Folder structure:

  • src
    • Server.ts
    • App.ts
  • test
    • test classes
  • package.json
  • tsconfig.json

package.json

{
  "name": "fallen",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "test": "mocha --require ts-node/register --watch-extensions ts './test/**/*.ts' --recursive -r tsconfig-paths/register './test/**/*.ts'",
    "dev": "ts-node ./src/server.ts",
    "start": "nodemon ./dist/server.js",
    "prod": "npm run build && npm run start"
  },
  "author": "Jim Pouwels",
  "license": "",
  "dependencies": {
    "@types/chai-http": "^3.0.5",
    "@types/express": "^4.16.0",
    "@types/pg": "^7.4.11",
    "body-parser": "^1.18.3",
    "chai-http": "^4.2.0",
    "express": "^4.16.4",
    "nodemon": "^1.18.9",
    "pg": "^7.7.1",
    "pg-promise": "^8.5.4"
  },
  "devDependencies": {
    "@types/chai": "^4.1.7",
    "@types/mocha": "^5.2.5",
    "chai": "^4.2.0",
    "mocha": "^5.2.0",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.7.0",
    "typescript": "^3.2.2"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "pretty": true,
        "sourceMap": true,
        "target": "es6",
        "outDir": "dist",
        "baseUrl": "./src",
        "resolveJsonModule": true,
        "esModuleInterop": true
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

VSCode accepts this, and also compiling 'tsc' works as a charm, so far so good. Even running unit tests with 'npm test' works, although I think that got fixed by adding '-r tsconfig-paths/register './test/**/*.ts'' to tsconfig.json.

However, when running with 'npm run dev' it fails to load module 'App'.

Server.ts

import app from 'App';
const PORT = 8080;

var service = app.expressApp.listen(PORT, () => {
    console.log('Express server listening on port ' + PORT);
})

I've read/found that this is because NodeJS has no knowledge of these absolute paths, but how can this be fixed?

There's no way I'm going to use these dreadful relative paths everywhere ;)

Error:

npm run dev

> fallen@1.0.0 dev ~/fallen
> ts-node ./src/server.ts

Error: Cannot find module 'App'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._resolveFilename (~/fallen/node_modules/module-alias/index.js:49:29)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (~/fallen/src/server.ts:2:1)
    at Module._compile (module.js:652:30)
    at Module.m._compile (~/fallen/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .ts] (~/fallen/node_modules/ts-node/src/index.ts:442:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! fallen@1.0.0 dev: `ts-node ./src/server.ts`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the fallen@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/jim/.npm/_logs/2019-01-05T09_27_41_657Z-debug.log```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions