egg.js 路径别名配置 module-alias

本文详细介绍如何在Node.js项目中使用module-alias模块进行路径别名的配置,包括安装依赖、配置package.json及app.js文件,以及在控制器中进行测试的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装依赖 moudle-alias

npm install module-alias --save

配置package.json文件

注:这里@root就是别名,后面引号内的内容就是原路径

"_moduleAliases": {
  "@root": ".",
  "@app": "app"
},

配置文件引入 require(‘module-alias/register’);

注:因为需要将该代码放置在所有使用路径别名代码的前面,因此在这里将此引用放置在app.js中,当然,也可以在所需的地方进行单独引用(不推荐)

'use strict';

class AppBootHook {
  constructor(app) {
    this.app = app;
  }

  configWillLoad() {
    // 此时 config 文件已经被读取并合并,但是还并未生效
    // 这是应用层修改配置的最后时机
    // 注意:此函数只支持同步调用
    require('module-alias/register');
    // 例如:参数中的密码是加密的,在此处进行解密

  }
}

module.exports = AppBootHook;

测试

/** app/controller/core/base.js */
'use strict';

const { Controller } = require('egg');

class BaseController extends Controller {

  success(data) {
    this.ctx.body = {
      sta: '999',
      msg: '操作成功',
      data,
    };
  }

}

module.exports = BaseController;

/** app/controller/moudleAliasCeshi.js */
'use strict';

const BaseController = require('@app/controller/core/base');

class MoudleAliasCeshiController extends BaseController {

  async ceshi() {
    this.success('dfsgvds');
  }

}

module.exports = MoudleAliasCeshiController;

/** app/router.js */
'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;

  router.get('/moudleAliasCeshi', controller.moudleAliasCeshi.ceshi);

};

Collecting llvmlite<0.32.0,>=0.31.0dev0 Using cached https://pypi.tuna.tsinghua.edu.cn/packages/17/fc/da81203725cb22d53e4f819374043bbfe3327831f3cb4388a3c020d7a497/llvmlite-0.31.0.tar.gz (110 kB) Requirement already satisfied: setuptools in /home/huxin/anaconda3/lib/python3.9/site-packages (from numba==0.48.0) (56.1.0) Building wheels for collected packages: llvmlite Building wheel for llvmlite (setup.py) ... error ERROR: Command errored out with exit status 1: command: /home/huxin/anaconda3/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/setup.py'"'"'; __file__='"'"'/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-4p6v4tbp cwd: /tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ Complete output (26 lines): running bdist_wheel /home/huxin/anaconda3/bin/python /tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py LLVM version... Traceback (most recent call last): File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 105, in main_posix out = subprocess.check_output([llvm_config, '--version']) File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 424, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 505, in run with Popen(*popenargs, **kwargs) as process: File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 951, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 1821, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'llvm-config' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 168, in <module> main() File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 158, in main main_posix('linux', '.so') File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 107, in main_posix raise RuntimeError("%s failed executing, please point LLVM_CONFIG " RuntimeError: llvm-config failed executing, please point LLVM_CONFIG to the path for llvm-config error: command '/home/huxin/anaconda3/bin/python' failed with exit code 1 ---------------------------------------- ERROR: Failed building wheel for llvmlite Running setup.py clean for llvmlite Failed to build llvmlite Installing collected packages: llvmlite, numba Attempting uninstall: llvmlite Found existing installation: llvmlite 0.38.0 Uninstalling llvmlite-0.38.0: Successfully uninstalled llvmlite-0.38.0 Running setup.py install for llvmlite ... error ERROR: Command errored out with exit status 1: command: /home/huxin/anaconda3/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/setup.py'"'"'; __file__='"'"'/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-0xp2acyx/install-record.txt --single-version-externally-managed --compile --install-headers /home/huxin/anaconda3/include/python3.9/llvmlite cwd: /tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ Complete output (29 lines): running install running build got version from file /tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/llvmlite/_version.py {'version': '0.31.0', 'full': 'fe7d985f6421d87f613bd414479d29d912771562'} running build_ext /home/huxin/anaconda3/bin/python /tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py LLVM version... Traceback (most recent call last): File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 105, in main_posix out = subprocess.check_output([llvm_config, '--version']) File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 424, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 505, in run with Popen(*popenargs, **kwargs) as process: File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 951, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/home/huxin/anaconda3/lib/python3.9/subprocess.py", line 1821, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'llvm-config' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 168, in <module> main() File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 158, in main main_posix('linux', '.so') File "/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/ffi/build.py", line 107, in main_posix raise RuntimeError("%s failed executing, please point LLVM_CONFIG " RuntimeError: llvm-config failed executing, please point LLVM_CONFIG to the path for llvm-config error: command '/home/huxin/anaconda3/bin/python' failed with exit code 1 ---------------------------------------- Rolling back uninstall of llvmlite Moving to /home/huxin/anaconda3/lib/python3.9/site-packages/llvmlite from /home/huxin/anaconda3/lib/python3.9/site-packages/~lvmlite Moving to /home/huxin/anaconda3/lib/python3.9/site-packages/llvmlite-0.38.0-py3.9.egg-info from /home/huxin/anaconda3/lib/python3.9/site-packages/~lvmlite-0.38.0-py3.9.egg-info ERROR: Command errored out with exit status 1: /home/huxin/anaconda3/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/setup.py'"'"'; __file__='"'"'/tmp/pip-install-fji4b7f3/llvmlite_2fba7ae59d6540769239538c32752489/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-0xp2acyx/install-record.txt --single-version-externally-managed --compile --install-headers /home/huxin/anaconda3/include/python3.9/llvmlite Check the logs for full command output.
最新发布
07-26
<think>我们正在解决安装numba时llvmlite编译失败的问题,错误信息是找不到llvm-config。根据引用[2]的解决方案,我们需要确保系统中有llvm-config,并且设置正确的环境变量指向它。 步骤: 1. 首先,我们需要安装LLVM开发包。根据引用[2],使用了llvm-config-3.9,所以我们可以尝试安装对应版本的LLVM(例如3.9版本)。 2. 如果已经安装了LLVM但找不到llvm-config,可能是因为可执行文件的名字不是llvm-config而是带有版本号(如llvm-config-3.9),此时我们可以创建一个别名alias)或者设置环境变量LLVM_CONFIG指向正确的路径。 具体操作: 对于Ubuntu/Debian系统: ```bash sudo apt-get install llvm-3.9-dev # 安装LLVM 3.9开发包 ``` 安装后,通常llvm-config会位于/usr/bin/llvm-config-3.9。然后设置环境变量: ```bash export LLVM_CONFIG=/usr/bin/llvm-config-3.9 ``` 然后重新安装llvmlite(建议先卸载再安装): ```bash pip uninstall llvmlite pip install llvmlite ``` 如果系统是CentOS,可以使用yum安装: ```bash sudo yum install llvm-devel # 但注意,这样安装的版本可能不是3.9,需要确认版本 ``` 如果yum安装的版本不是3.9,我们可以从源码编译安装LLVM 3.9,或者寻找包含3.9版本的仓库。 另外,引用[3]中提到了类似的问题(pycurl找不到curl-config),解决方法是安装curl的开发包(libcurl-devel)。同理,这里我们需要安装LLVM的开发包(llvm-devel)并确保llvm-config在PATH中。 如果安装后仍然找不到,可以尝试创建符号链接: ```bash sudo ln -s /usr/bin/llvm-config-3.9 /usr/bin/llvm-config ``` 这样,llvm-config命令就可用了。 注意:numba对llvmlite的版本有要求,而llvmlite对LLVM的版本也有严格要求。根据llvmlite的官方文档,不同版本的llvmlite需要特定版本的LLVM。例如,llvmlite 0.31.0需要LLVM 7.0或7.1,而较新的版本可能需要更新的LLVM。因此,我们需要查看要安装的llvmlite版本所要求的LLVM版本。 如果上述方法不行,可以考虑从llvmlite的源码安装,并指定LLVM_CONFIG环境变量。 总结步骤: 1. 安装对应版本的LLVM(包括开发包,确保有llvm-config-xxx)。 2. 设置环境变量LLVM_CONFIG指向llvm-config的绝对路径(或者创建符号链接使llvm-config命令可用)。 3. 重新安装llvmlite。 如果用户使用的是虚拟环境,请确保环境变量设置正确,并且在虚拟环境中生效。 相关问题: 1. 如何检查系统上安装的LLVM版本? 2. 如何从源码编译安装特定版本的LLVM? 3. 如何设置永久的环境变量(而不是每次在终端临时设置)?</think>### 解决 llvmlite 安装错误:缺少 llvm-config 安装 llvmlite 时遇到 `FileNotFoundError: llvm-config` 错误,通常是因为系统缺少 LLVM 开发工具或环境变量未正确配置。以下是系统级解决方案: --- ### **解决方法** #### 1. **安装 LLVM 开发包** 不同操作系统的安装命令: - **Ubuntu/Debian**: ```bash sudo apt-get install llvm-11-dev llvm-11 # 推荐安装 LLVM 11(numba 兼容版本) ``` - **CentOS/RHEL**: ```bash sudo yum install llvm-devel llvm-static # 安装默认版本 ``` - **macOS (Homebrew)**: ```bash brew install llvm@11 # 安装指定版本 ``` #### 2. **配置环境变量** 设置 `LLVM_CONFIG` 路径指向已安装的 `llvm-config`: ```bash # 查找 llvm-config 路径(通常在 /usr/bin/ 或 /usr/local/opt/llvm/bin/) which llvm-config # 若存在,输出路径如 /usr/bin/llvm-config-11 # 设置临时环境变量(当前终端生效) export LLVM_CONFIG=/usr/bin/llvm-config-11 # 替换为实际路径 # 永久生效(添加到 ~/.bashrc 或 ~/.zshrc) echo 'export LLVM_CONFIG=/usr/bin/llvm-config-11' >> ~/.bashrc source ~/.bashrc ``` #### 3. **重新安装 llvmlite** ```bash # 先卸载旧版本 pip uninstall -y llvmlite # 安装时指定环境变量 LLVM_CONFIG=/usr/bin/llvm-config-11 pip install llvmlite --no-cache-dir ``` #### 4. **验证安装** ```python python -c "import llvmlite; print(llvmlite.__version__)" # 输出版本号即成功 ``` --- ### **常见问题排查** - **问题 1:`llvm-config` 存在但版本不兼容** llvmlite 要求特定 LLVM 版本(如 0.40.x 需 LLVM 10~11[^1])。解决: ```bash # 安装正确版本 sudo apt-get install llvm-11-dev # 创建符号链接 sudo ln -s /usr/bin/llvm-config-11 /usr/bin/llvm-config ``` - **问题 2:多版本 LLVM 冲突** 若系统有多个 LLVM,显式指定路径: ```bash LLVM_CONFIG=/opt/llvm/11.1.0/bin/llvm-config pip install llvmlite ``` - **问题 3:权限不足** 使用 `sudo` 安装开发包,但避免 `sudo pip`(可能导致权限混乱)。 --- ### **引用说明** > 类似错误在安装 `pycurl` 时因缺失 `curl-config` 也会出现[^3],原理均为开发工具链未安装。通过安装对应开发包(如 `libcurl-devel`)并配置路径可解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值