You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Nix与Flake构建React项目遇权限错误的解决问询

解决Nix构建React应用时的ESLint权限问题

问题场景

使用React开发Web应用,通过Nix与Flake构建时出现以下错误:

error: builder for '/nix/store/my-app.drv' failed with exit code 1;
       last 10 log lines:
       > is not maintianed anymore. It is thus unlikely that this bug will
       > ever be fixed. Add "@babel/plugin-proposal-private-property-in-object" to
       > your devDependencies to work around this error. This will make this message
       > go away.
       >
       > Failed to compile.
       > 
       > [eslint] EACCES: permission denied, mkdir '/build/auth/node_modules/.cache'
       >
       >
       For full logs, run '/nix/store/my-app.drv'.

项目结构:

├── default.nix
├── flake.nix
└── site
    └── client
        └── auth
            ├── default.nix
            ├── nix
            │   ├── default.nix
            │   ├── node-env.nix
            │   └── node-packages.nix
            ├── ... # React应用文件
            └── node_modules               

当前构建配置(./site/client/auth/default.nix):

static = stdenv.mkDerivation {
    name = "my-client";
    src = ./.;
    buildInputs = [ nodejs ];
    buildPhase = ''
      ln -s ${generated.nodeDependencies}/lib/node_modules ./node_modules
      export PATH="${generated.nodeDependencies}/bin:$PATH"
      npm run build
    '';
    installPhase = ''
      cp -r dist $out/
    '';
  };

解决方案

1. 修复ESLint权限错误

错误核心是:node_modules是指向Nix Store的符号链接(Nix Store为只读目录),ESLint尝试在其中创建.cache目录导致权限不足。解决方式是指定ESLint使用可写的临时目录作为缓存路径:

修改buildPhase,添加环境变量配置:

buildPhase = ''
  ln -s ${generated.nodeDependencies}/lib/node_modules ./node_modules
  export PATH="${generated.nodeDependencies}/bin:$PATH"
  # 让ESLint将缓存放到临时目录
  export ESLINT_CACHE_DIR=$(mktemp -d)
  npm run build
'';

另一种方式是在React项目的ESLint配置文件(如.eslintrc.js)中直接指定缓存位置:

module.exports = {
  // ...其他配置
  cache: true,
  cacheLocation: './.eslintcache' // 选择项目内可写路径,Nix构建时优先推荐临时目录方案
};

2. 处理Babel提示

根据错误日志,需添加指定Babel插件到开发依赖:
在项目package.json的devDependencies中加入:

"@babel/plugin-proposal-private-property-in-object": "^7.21.0"

之后重新生成node-packages.nix(若使用node2nix等工具)。

额外优化

Nix构建时,建议排除本地node_modules目录,避免干扰构建流程。可在项目根目录创建.nixignore文件:

node_modules

内容的提问来源于stack exchange,提问作者Fabian Matias Vega Alcota

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 05:32:54