[英]Node unable to find module alias defined in tsconfig and package.json
I'm trying to run a typescript project in production mode.我正在尝试在生产模式下运行 typescript 项目。 The code is here on Github
代码在 Github 上
npm run start:dev
is running the server up on http://localhost:3000/npm run start:dev
在http://localhost:3000/上运行服务器npm run build
is generating the dist
foldernpm run build
正在生成dist
文件夹The package.json
has the definition of @server
defined using module alias : package.json
具有使用模块别名定义的@server
定义:
"_moduleAliases": {
"@server": "dist/Server"
}
The @server
is defined under compilerOptions.paths.@server
in tsconfig.json
too! @server
也在tsconfig.json
的compilerOptions.paths.@server
下定义!
The index.ts
imports app
from @server
; index.ts
从@server
导入app
;
import app from '@server';
When I run npm start
- it does nothing当我运行
npm start
- 它什么也不做
So I tried node dist/index.js --env=production
and it throws the following error:所以我尝试了
node dist/index.js --env=production
并抛出以下错误:
Error: Cannot find module '@server'
Why is the node not detecting this module alias ?为什么节点没有检测到这个模块别名?
As stated on the module-alias
README , it changes the default behavior of require
.正如
module-alias
README中所述,它改变了require
的默认行为。 So, to use it, you have to add require('module-alias/register')
to the beginning of your code before you import anything.因此,要使用它,您必须在导入任何内容之前将
require('module-alias/register')
添加到代码的开头。 I would suggest adding it to LoadEnv.ts
.我建议将其添加到
LoadEnv.ts
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.