Error message:
Uncaught ReferenceError: require is not defined
at index.js?5WdmUIncGTkIrWhONvlEDQ:1:1
(anonymous) @ index.js?5WdmUIncGTkIrWhONvlEDQ:1
First lines of index.js:
require("./index.css");
var $cI6W1$lodash = require("lodash");
var $cI6W1$react = require("react");
var $cI6W1$reactdom = require("react-dom");
var $cI6W1$swchelperslib_define_propertyjs = require("@swc/helpers/lib/_define_property.js");
var $cI6W1$chrononode = require("chrono-node");
Here is my package.json file:
{
"name": "stacks-components",
"version": "1.0.0",
"description": "Components for Stacks CMS",
"private": true,
"main": "../public/js/index.js",
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": {
"start": "parcel start app/index.jsx --host localhost --public-url /js",
"build": "parcel build app/index.jsx --public-url /js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@parcel/transformer-sass": "^2.8.3",
"babel-eslint": "^10.0.3",
"eslint": "^5.16.0",
"eslint-plugin-react": "^7.17.0",
"postcss": "^8.4.21",
"postcss-modules": "^4.3.1"
},
"dependencies": {
"@babel/core": "^7.7.7",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/preset-react": "^7.7.4",
"@draft-js-plugins/editor": "^4.1.0",
"@swc/helpers": "^0.4.14",
"autoprefixer": "^9.7.3",
"chrono-node": "^2.3.8",
"classnames": "^2.2.6",
"dompurify": "^2.3.0",
"draft-js": "^0.11.7",
"draft-js-export-html": "^1.4.1",
"draft-js-plugins-editor": "^3.0.0",
"draggable": "^4.2.0",
"dropzone": "^5.7.2",
"linkify-html": "^3.0.5",
"lodash": "^4.17.15",
"openai": "^3.1.0",
"parcel": "^2.8.3",
"parcel-bundler": "^1.12.5",
"react": "^16.13.1",
"react-datetime-picker": "^3.0.4",
"react-dom": "^16.12.0",
"react-dropzone": "^11.2.0",
"react-fetch-component": "^7.0.1",
"react-router-dom": "^5.1.2",
"react-select": "^3.1.0",
"react-sortablejs": "^2.0.11",
"ress": "^2.0.3",
"rimraf": "^2.7.1",
"sass": "^1.24.4",
"trix": "^1.2.3",
"uuid": "^8.3.1"
},
"_id": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b080f1a181008561814160b14151e150f083b4a554b554b">[email protected]</a>"
}
a sample of app/index.jsx
import _ from "lodash";
import React from "react";
import { render } from "react-dom";
import DateRange from "./components/DateRange";
import DraftJS from "./components/DraftJS";
import Supertag from "./components/Supertag";
// ...
// Use this file to mount all the components defined in the components folder
Dropzone.autoDiscover = false;
function dateRangeInit() {
document.querySelectorAll(".date-range-react").forEach((element) => {
render(
React.createElement(DateRange, {
fromVal: element.dataset.fromval,
fromFieldName: element.dataset.fromfieldname,
fromLabel: element.dataset.fromlabel,
toVal: element.dataset.toval,
toFieldName: element.dataset.tofieldname,
toLabel: element.dataset.tolabel,
recordType: element.dataset.recordtype,
}),
element
);
});
}
dateRangeInit();
...
I am able to successfully build, and I can confirm that the JS and CSS files are being generated in the proper location. But the "require" error appears in the browser when I try to load the page.
I am seeing this error as I attempt to upgrade from Parcel 1 to 2. This error was not happening in Parcel 1.
My use case is that I have a Ruby on Rails web app, but I use Parcel to build some ReactJS components. The index.js
and index.css
file is loaded on each page in the Rails app.