Mr.X
Mr.X

Reputation: 31345

Node unable to find module alias defined in tsconfig and package.json

I'm trying to run a typescript project in production mode. The code is here on Github

The package.json has the definition of @server defined using module alias:

"_moduleAliases": {
  "@server": "dist/Server"
}

The @server is defined under compilerOptions.paths.@server in tsconfig.json too!

The index.ts imports app from @server;

import app from '@server';

When I run npm start - it does nothing

So I tried node dist/index.js --env=production and it throws the following error:

Error: Cannot find module '@server'

Why is the node not detecting this module alias?

Upvotes: 3

Views: 12067

Answers (1)

Aplet123
Aplet123

Reputation: 35560

As stated on the module-alias README, it changes the default behavior of require. So, to use it, you have to add require('module-alias/register') to the beginning of your code before you import anything. I would suggest adding it to LoadEnv.ts.

Upvotes: 9

Related Questions