uni-app Uncaught ReferenceError: process is not defined at app-renderjs.js

发布于 1周前 作者 bupafengyu 来自 Uni-App

uni-app Uncaught ReferenceError: process is not defined at app-renderjs.js

产品分类:uniapp/App

开发环境信息

项目 详情
PC开发环境操作系统 Windows
PC开发环境操作系统版本号 win11
HBuilderX类型 正式
HBuilderX版本号 4.45
手机系统 Android
手机系统版本号 Android 15
手机厂商 华为
手机机型 ipad
页面类型 vue
vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

bug描述

11:46:24.131 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:25.862 [1736221587340][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587341][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587342][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.102 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:26.461 [1736221587936][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.493 output->services,  [Object] [{"id":"univerify","description":"一键登录","nativeClient":true,"authResult":null,"userInfo":nu...} at utils/index.js:322
11:46:26.685 Uncaught ReferenceError: process is not defined at app-renderjs.js:54

示例代码

11:46:24.131 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:25.862 [1736221587340][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587341][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587342][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.102 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:26.461 [1736221587936][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.493 output->services,  [Object] [{"id":"univerify","description":"一键登录","nativeClient":true,"authResult":null,"userInfo":nu...} at utils/index.js:322
11:46:26.685 Uncaught ReferenceError: process is not defined at app-renderjs.js:54

操作步骤

11:46:24.131 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:25.862 [1736221587340][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587341][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587342][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.102 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:26.461 [1736221587936][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.493 output->services,  [Object] [{"id":"univerify","description":"一键登录","nativeClient":true,"authResult":null,"userInfo":nu...} at utils/index.js:322
11:46:26.685 Uncaught ReferenceError: process is not defined at app-renderjs.js:54

预期结果

11:46:24.131 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:25.862 [1736221587340][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587341][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587342][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.102 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:26.461 [1736221587936][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.493 output->services,  [Object] [{"id":"univerify","description":"一键登录","nativeClient":true,"authResult":null,"userInfo":nu...} at utils/index.js:322
11:46:26.685 Uncaught ReferenceError: process is not defined at app-renderjs.js:54

实际结果

11:46:24.131 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:25.862 [1736221587340][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587341][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:25.862 [1736221587342][1ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.102 Uncaught ReferenceError: process is not defined at app-renderjs.js:54
11:46:26.461 [1736221587936][0ms][renderjs]:"2cd3a838 not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.461 [1736221587936][0ms][wxs]:"module AppIndex not found" at uni-app-view.umd.js:7
11:46:26.493 output->services,  [Object] [{"id":"univerify","description":"一键登录","nativeClient":true,"authResult":null,"userInfo":nu...} at utils/index.js:322
11:46:26.685 Uncaught ReferenceError: process is not defined at app-renderjs.js:54

4 回复

写了什么代码出现的?提供一个空白工程。看报错是使用了 process 这个 node 的环境变量


定位到问题了,项目是由wap2html过来的,以前就安装了html2canvas依赖,都运行一年了,都从来没有出现这个错,昨天重构就把不要的包html2canvas删除,然后运行就一直报这个错,项目里也没有地方用到它,找遍了没找到,最后没办法,安装这个包又能正常运行了。

回复 b***@qq.com: 可能是 html2canvas 对 process 进行了赋值,你可以搜一下源码,应该是为了兼容 node 和浏览器

在uni-app中遇到Uncaught ReferenceError: process is not defined这类错误通常是因为代码中使用了process这个全局变量,而process是Node.js环境中的一个全局对象,它并不存在于浏览器环境中,包括uni-app运行的环境。

uni-app主要用于开发跨平台的应用,其代码最终会运行在浏览器、小程序或其他非Node.js环境中。因此,如果你在代码中直接使用process对象,会导致上述错误。

要解决这个问题,你需要避免在uni-app中直接使用process对象。如果你需要判断当前环境或者进行环境相关的配置,可以使用其他方式。以下是一些可能的解决方案:

1. 使用条件编译

你可以使用uni-app的条件编译功能来区分不同的运行环境。例如,你可以在app-renderjs.js中通过条件编译来避免使用process对象:

// #ifdef H5
// 在H5环境下使用特定的代码
// 这里可以定义一些全局变量或者函数来替代process的功能
const isProduction = window.location.hostname.indexOf('your-production-domain') !== -1;
// #endif

// #ifdef MP-WEIXIN
// 在微信小程序环境下使用特定的代码
// 微信小程序环境下不需要处理process相关的代码
// #endif

// 其他代码

2. 使用环境变量

你也可以通过配置环境变量来区分不同的环境。在uni-app中,你可以通过manifest.json文件配置环境变量:

{
  "mp-weixin": {
    "env": {
      "NODE_ENV": "production"
    }
  },
  "h5": {
    "env": {
      "NODE_ENV": "development"
    }
  }
}

然后在代码中通过process.env.NODE_ENV访问这些环境变量(注意,这里process仍然不能直接使用,你需要通过其他方式传递环境变量):

// 由于uni-app不支持直接使用process.env,这里需要其他方式来获取环境变量
const NODE_ENV = getEnvVariable('NODE_ENV'); // 假设你有一个函数getEnvVariable来获取环境变量

if (NODE_ENV === 'production') {
  // 生产环境代码
} else {
  // 开发环境代码
}

注意:由于uni-app不支持直接使用Node.js的全局变量如process,你需要自己实现获取环境变量的方法(例如通过全局变量、配置文件等方式)。

通过上述方法,你可以避免在uni-app中使用process对象,从而解决Uncaught ReferenceError: process is not defined的错误。

回到顶部