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

Shopify PHP-React生产环境报错:Uncaught ReferenceError: require is not defined

Shopify PHP+React应用生产环境构建后出现"require is not defined"错误

本地开发时,基于Shopify PHP模板与React前端模板创建的应用运行正常,但切换到生产环境后出现问题:生产环境以public目录的index.html为入口,执行npm run build通过Vite构建JS文件后,访问页面抛出错误:

Uncaught ReferenceError: require is not defined

错误指向代码行var wb = require("react"),但我并未在代码中使用require语法,且使用的是全新的Shopify PHP模板,推测问题出在依赖配置或Vite构建环节。

package.json配置

{
  "name": "shopify-frontend-template-react",
  "version": "1.0.0",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "vite build",
    "dev": "vite",
    "coverage": "vitest run --coverage"
  },
  "type": "module",
  "engines": {
    "node": ">= 12.16"
  },
  "stylelint": {
    "extends": "@shopify/stylelint-polaris"
  },
  "dependencies": {
    "@formatjs/intl-locale": "^3.3.2",
    "@formatjs/intl-localematcher": "^0.4.0",
    "@formatjs/intl-pluralrules": "^5.2.4",
    "@shopify/app-bridge": "^3.7.7",
    "@shopify/app-bridge-react": "^3.7.7",
    "@shopify/i18next-shopify": "^0.2.3",
    "@shopify/polaris": "^10.49.1",
    "@vitejs/plugin-react": "1.2.0",
    "i18next": "^23.1.0",
    "i18next-resources-to-backend": "^1.1.4",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-i18next": "^13.0.0",
    "react-query": "^3.34.19",
    "react-router-dom": "^6.3.0",
    "vite": "^4.3.9"
  },
  "devDependencies": {
    "@shopify/stylelint-polaris": "^12.0.0",
    "history": "^5.3.0",
    "jsdom": "^19.0.0",
    "prettier": "^2.6.0",
    "stylelint": "^15.6.1",
    "vi-fetch": "^0.6.1"
  }
}

已尝试的无效解决方案

  • 在package.json中添加/移除"type"字段
  • 将vite.config.js重命名为vite.config.mjs
  • 在Index.jsx中显式导入React
  • 改用react-app和react-app-rewired构建应用

使用的模板

  • npm init @shopify/app@3.48.4 -- --template php
  • Shopify官方PHP应用模板

内容的提问来源于stack exchange,提问作者Saqib Rashid

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 06:05:56