0

I'm recently developing web app with React and using "nedb" for the database. I can run the below codes with $ node "the code path". Though, in localhost, the below error is occurred.

ERROR
process is not defined
ReferenceError: process is not defined
    at ./node_modules/path/path.js (H//localhost:3000/static/js/bundle.js:37516:17)
    at options.factory (H//localhost:3000/static/js/bundle.js:81477:31)
    at __webpack_require__ (H//localhost:3000/static/js/bundle.js:80900:33)
    at fn (H//localhost:3000/static/js/bundle.js:81134:21)
    at ./node_modules/nedb/lib/persistence.js (H//localhost:3000/static/js/bundle.js:35644:10)
    at options.factory (H//localhost:3000/static/js/bundle.js:81477:31)
    at __webpack_require__ (H//localhost:3000/static/js/bundle.js:80900:33)
    at fn (H//localhost:3000/static/js/bundle.js:81134:21)
    at ./node_modules/nedb/lib/datastore.js (H//localhost:3000/static/js/bundle.js:33587:17)
    at options.factory (H//localhost:3000/static/js/bundle.js:81477:31)

I changed the URL in the error since StackOverFlow rejected this as spam. (https->H)

DatabaseComponent.js

const NeDB = require('nedb');

const db = {};
db.users = new NeDB({
  filename: 'src/database/users.db',
  autoload: true,
});

export const addUser = (props) => {
  const { name, email } = props;
  db.users.insert({ name, email }, (err, newDoc) => {
    console.log('[INSERT]');
    console.log(newDoc);
  });
};

export default { addUser };

EditDB.js

/* eslint-disable react/button-has-type */
import { React } from 'react';
import { useNavigate } from 'react-router-dom';
// eslint-disable-next-line no-unused-vars
import { addUser } from '../../backend/DatabaseComponent';

function EditDB() {
  return (
    <div className="editDB">
      <h1>EditDB</h1>
      <Buttons />
    </div>
  );
}

function Buttons() {
  const navigate = useNavigate();
  return (
    <div className="buttons">
      <button onClick={() => navigate(-1)}>back</button>
      <button onClick={({}) => addUser()}>create</button>
    </div>
  );
}

export default EditDB;

package.json

{
  "name": "imse2",
  "version": "0.1.0",
  "private": true,
  "homepage": "./",
  "dependencies": {
    "@emotion/react": "^11.11.1",
    "@emotion/styled": "^11.11.0",
    "@mui/icons-material": "^5.11.16",
    "@mui/material": "^5.13.5",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "nedb": "^1.8.0",
    "path": "^0.12.7",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-icons": "^4.9.0",
    "react-router-dom": "^6.13.0",
    "react-scripts": "5.0.1",
    "util": "^0.12.5",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "eslint": "^8.43.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-plugin-import": "^2.27.5",
    "eslint-plugin-jsx-a11y": "^6.7.1",
    "eslint-plugin-react": "^7.32.2",
    "eslint-plugin-react-hooks": "^4.6.0"
  }
}

I googled this error and found that many people solved this error by modifying webpack.config.js. Though there isn't the file in my project perhaps because I created this app with create-react-app. Can someone please enlighten me on the solution to this error? Thank you in advance.

mksmkss
  • 1
  • 2

1 Answers1

0

[solved]

Step 1: I uninstalled "path", and then the below error was shown.

The error

If you want to include a polyfill, you need to:
 - add a fallback 
'resolve.fallback: { 
"path": require.resolve("path-browserify") 
}' 
- install 'path-browserify' 
If you don't want to include a polyfill, you can use an empty module like this:
 resolve.fallback: { "path": false }

This error is caused by the version up of webpack(check this out: link)

Also, I found webpack.config.js in /node_modules/react-scripts/config/webpack.config.js.

Step 2: I added some scripts in this file:

resolve:{
 ...
 fallback: {
        "fs": false,
        "tls": false,
        "net": false,
        "path": false,
        "zlib": false,
        "http": false,
        "https": false,
        "stream": false,
        "crypto": false,
      }
}

Step 3: After editing, try to restart npm. Your work will work!!

And, Thank you, Mr. Lin.

mksmkss
  • 1
  • 2