jprichardson / node-jsonfile

Easily read/write JSON files.
MIT License
1.2k stars 321 forks source link

Plugin is not working in vue js #140

Open sadashivm opened 3 years ago

sadashivm commented 3 years ago

Hello, Im trying to read/write json file, but it is not working, Im getting below error.

image

image

image

image

I Kept data.json same path where App.vue is exist.

RyanZim commented 3 years ago

jsonfile is not for use in the browser; only in Node or Electron.

sadashivm commented 3 years ago

Thanks for the reply @RyanZim ,

I'm using this plugin with Electron only. I got the below error, image

Here is my code,

image

NOTE: Book.js and data.json files are in the same folder and I have used react js for coding.

RyanZim commented 3 years ago

OK, what bundler are you using? Webpack? Does importing fs directly and using writeFile work?

sadashivm commented 3 years ago

OK, what bundler are you using? Webpack? Does importing fs directly and using writeFile work?

@RyanZim , Where should I know which bundler I'm using? I don't know exactly. here is my package.json file,

package.json

{
  "name": "test-electron-app",
  "version": "0.1.0",
  "description": "Test App",
  "author": "sadashiv",
  "main": "public/electron.js",
  "build": {
    "appId": "com.test-electron-app"
  },
  "homepage": "./",
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.6",
    "@testing-library/react": "^11.2.2",
    "@testing-library/user-event": "^12.2.2",
    "@tinymce/tinymce-react": "^3.8.1",
    "bootstrap": "^4.5.3",
    "cross-env": "^7.0.2",
    "electron-is-dev": "^1.2.0",
    "jsonfile": "^6.1.0",
    "node-sass": "^4.14.1",
    "react": "^17.0.1",
    "react-bootstrap": "^1.4.0",
    "react-bootstrap-icons": "^1.1.0",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.1",
    "smooth-scrollbar": "^8.5.3",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "react-start": "react-scripts start",
    "react-build": "react-scripts build",
    "react-test": "react-scripts test",
    "react-eject": "react-scripts eject",
    "electron-build": "electron-builder",
    "build": "npm run react-build && npm run electron-build",
    "start": "concurrently \"cross-env BROWSER=none npm run react-start\" \"wait-on http://localhost:3000 && electron .\""
  },
  "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": {
    "concurrently": "^5.3.0",
    "electron": "^11.0.3",
    "electron-builder": "^22.9.1",
    "wait-on": "^5.2.0"
  }
}

Can I use ur plugin in development mode? Will it work?

Can you please tel steps to how to use ur npm package with electron and reactjs ?

RyanZim commented 3 years ago

Ah, you're using react-scripts, which uses webpack under the hood.

Comment out your usage of jsonfile, and put this somewhere in your code:

import fs from 'fs';
fs.writeFile('test.txt', 'Hello World!', (err) => {
  if (err) console.error(err);
});

Run your app, see if you get an error, and if the file test.txt is created with contents Hello World!. Let me know what happens; then we can debug from there.