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

Netlify执行npm run generate部署Nuxt项目时出现模块缺失错误

问题:Netlify部署Nuxt.js项目时出现"Cannot find module 'node:fs'"错误

几年前我用Nuxt.js开发了个人作品集并部署在Netlify平台,今天仅更新了部分文本内容提交到GitLab仓库后,部署失败,日志中出现Cannot find module 'node:fs'错误。我未修改过Netlify配置,之前部署一直正常,尝试过修改构建命令为CI= npm run generate和设置环境变量CI=false,但问题未解决,现已恢复原配置。

部署错误日志

1:21:09 AM: ​
1:21:09 AM:   1. Build command from Netlify app                              
1:21:09 AM: ────────────────────────────────────────────────────────────────
1:21:09 AM: ​
1:21:09 AM: $ npm run generate
1:21:09 AM: > kevinburbano-devfolio@1.0.0 generate /opt/build/repo
1:21:09 AM: > nuxt generate
1:21:09 AM: internal/modules/cjs/loader.js:969
1:21:09 AM:   throw err;
1:21:09 AM:   ^
1:21:09 AM: Error: Cannot find module 'node:fs'
1:21:09 AM: Require stack:
1:21:09 AM: - /opt/build/repo/node_modules/rc9/dist/index.cjs
1:21:09 AM: - /opt/build/repo/node_modules/@nuxt/config/dist/config.js
1:21:09 AM: - /opt/build/repo/node_modules/@nuxt/cli/dist/cli-index.js
1:21:09 AM: - /opt/build/repo/node_modules/@nuxt/cli/dist/cli.js
1:21:09 AM: - /opt/build/repo/node_modules/nuxt/bin/nuxt.js
1:21:09 AM:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
1:21:09 AM:     at Function.Module._load (internal/modules/cjs/loader.js:842:27)
1:21:09 AM:     at Module.require (internal/modules/cjs/loader.js:1026:19)
1:21:09 AM:     at require (internal/modules/cjs/helpers.js:72:18)
1:21:09 AM:     at Object.<anonymous> (/opt/build/repo/node_modules/rc9/dist/index.cjs:3:17)
1:21:09 AM:     at Module._compile (internal/modules/cjs/loader.js:1138:30)
1:21:09 AM:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
1:21:09 AM:     at Module.load (internal/modules/cjs/loader.js:986:32)
1:21:09 AM:     at Function.Module._load (internal/modules/cjs/loader.js:879:14)
1:21:09 AM:     at Module.require (internal/modules/cjs/loader.js:1026:19) {
1:21:09 AM:   code: 'MODULE_NOT_FOUND',
1:21:09 AM:   requireStack: [
1:21:09 AM:     '/opt/build/repo/node_modules/rc9/dist/index.cjs',
1:21:09 AM:     '/opt/build/repo/node_modules/@nuxt/config/dist/config.js',
1:21:09 AM:     '/opt/build/repo/node_modules/@nuxt/cli/dist/cli-index.js',
1:21:09 AM:     '/opt/build/repo/node_modules/@nuxt/cli/dist/cli.js',
1:21:09 AM:     '/opt/build/repo/node_modules/nuxt/bin/nuxt.js'
1:21:09 AM:   ]
1:21:09 AM: }
1:21:09 AM: npm ERR! code ELIFECYCLE
1:21:09 AM: npm ERR! errno 1
1:21:09 AM: npm ERR! kevinburbano-devfolio@1.0.0 generate: `nuxt generate`
1:21:09 AM: npm ERR! Exit status 1
1:21:09 AM: npm ERR!
1:21:09 AM: npm ERR! Failed at the kevinburbano-devfolio@1.0.0 generate script.
1:21:09 AM: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
1:21:09 AM: npm ERR! A complete log of this run can be found in:
1:21:09 AM: npm ERR!     /opt/buildhome/.npm/_logs/2023-05-04T06_21_09_463Z-debug.log
1:21:09 AM: ​
1:21:09 AM:   "build.command" failed                                         
1:21:09 AM: ────────────────────────────────────────────────────────────────
1:21:09 AM: ​
1:21:09 AM:   Error message
1:21:09 AM:   Command failed with exit code 1: npm run generate
1:21:09 AM: ​
1:21:09 AM:   Error location
1:21:09 AM:   In Build command from Netlify app:
1:21:09 AM:   npm run generate
1:21:09 AM: ​
1:21:09 AM:   Resolved config
1:21:09 AM:   build:
1:21:09 AM:     command: npm run generate
1:21:09 AM:     commandOrigin: ui
1:21:09 AM:     publish: /opt/build/repo/dist
1:21:09 AM:     publishOrigin: ui
1:21:10 AM: Caching artifacts
1:21:10 AM: Started saving node modules
1:21:10 AM: Finished saving node modules
1:21:10 AM: Started saving build plugins
1:21:10 AM: Finished saving build plugins
1:21:10 AM: Started saving corepack cache
1:21:10 AM: Finished saving corepack cache
1:21:10 AM: Started saving pip cache
1:21:10 AM: Finished saving pip cache
1:21:10 AM: Started saving emacs cask dependencies
1:21:10 AM: Finished saving emacs cask dependencies
1:21:10 AM: Started saving maven dependencies
1:21:10 AM: Finished saving maven dependencies
1:21:10 AM: Started saving boot dependencies
1:21:10 AM: Finished saving boot dependencies
1:21:10 AM: Started saving rust rustup cache
1:21:10 AM: Finished saving rust rustup cache
1:21:10 AM: Started saving go dependencies
1:21:10 AM: Finished saving go dependencies
1:21:11 AM: Cached Ruby version 2.7.1
1:21:11 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
1:21:11 AM: Failing build: Failed to build site
1:21:11 AM: Finished processing build request in 54.015s

项目package.json内容

{
  "name": "kevinburbano-devfolio",
  "version": "1.0.0",
  "description": "Kevin Burbano developer portafolio",
  "author": "Kevin Burbano",
  "private": true,
  "scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate"
  },
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.15.2",
    "@nuxtjs/firebase": "^7.4.1",
    "bootstrap": "^5.0.0-beta1",
    "firebase": "^8.2.5",
    "hooper": "^0.3.4",
    "node-sass": "^5.0.0",
    "nuxt": "^2.0.0",
    "popper.js": "^1.14.3",
    "sass-loader": "^10.1.1",
    "toastr": "^2.1.4",
    "vue": "^2.6.12",
    "vue-scrollto": "^2.20.0"
  },
  "devDependencies": {
    "@nuxtjs/router": "^1.5.0"
  }
}

解决办法

1. 指定Node.js版本

node:fs是Node.js 16及以上版本才支持的模块导入写法,Netlify当前构建环境使用的Node版本可能低于16,导致无法识别该模块。可以通过以下方式指定版本:

  • 在项目根目录创建.nvmrc文件,写入16或更高版本(如18),提交到GitLab;
  • 或者在package.json中添加engines字段:
    "engines": {
      "node": ">=16.x"
    }
    

Netlify会自动读取配置,使用指定的Node版本进行构建。

2. 锁定依赖版本

由于项目未锁定依赖版本,Netlify构建时可能安装了最新版的rc9模块(该模块现在依赖node:fs),而旧版本的rc9并不需要这个写法。可以:

  • 运行npm install生成package-lock.json文件,将其提交到GitLab,确保Netlify安装与本地一致的依赖版本;
  • 或者如果使用yarn,生成yarn.lock并提交。

3. 降级rc9模块

如果不想升级Node版本,可以手动安装兼容旧Node版本的rc9版本,例如:

npm install rc9@2.1.1 --save-exact

然后更新package-lock.json并提交到仓库,这样Netlify会安装指定的旧版本rc9,避免使用需要node:fs的新版本。

内容的提问来源于stack exchange,提问作者Kevin Burbano

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 17:47:11