-3

I am making a game with html in node.js and want to have a json file containing all of the user's information (inventory, stats, etc). But when I try and using require, the console throws the error "Uncaught Error: Cannot find module 'playerstats.json'". I am using the electron package to run the app.

Here is the code:

var json = require('./playerstats.json')
var data = json.parse(json);

and here is the filesystem: enter image description here I am trying to read the json from char_creation.js

I tried changing the type string in package.json and it didn't work. I also tried using import to load it and fs.readfile and fs.readfilesync but those also didn't work.

  • 1
    Duplicate of [Node.JS cannot find json file, everything is right](https://stackoverflow.com/questions/62630608/node-js-cannot-find-json-file-everything-is-right) – esqew Aug 17 '23 at 21:16
  • You told Node to load a _module_ called `playerstats.json` from the `node_modules` directory. You need some extra bits if you want Node to actually load a file. See [is there a require for json in node.js](https://stackoverflow.com/questions/7163061/is-there-a-require-for-json-in-node-js) – Mike 'Pomax' Kamermans Aug 17 '23 at 21:21
  • I put ./ in front of the playerstats.json and it still throws the same error , just with './playerstats.json' instead of 'playerstats.json' – Feylin Urlala Aug 17 '23 at 21:23
  • @Mike'Pomax'Kamermans You were right, I had to start from the C: drive and work my way down – Feylin Urlala Aug 17 '23 at 21:55
  • No, you just needed the correct relative location. – Mike 'Pomax' Kamermans Aug 18 '23 at 05:11

0 Answers0