-1

So I'm trying to use this node package. The usage instructions state that I need to require a JSON file. Something like this :

var myschema = require('schema.json');

But when I run this it wants to find a node package by the name of 'schema.json' and throws an error "Cannot find module 'schema.json'". How do I avoid this error?

Parth Mody
  • 438
  • 1
  • 5
  • 17

1 Answers1

0

The name of the libray you linked to is json-schema-deref

As such, to import that library you would do:

var deRefLib = require('json-schema-deref')

Then to load your schema file you'd need to use a require with a relative path. If your schema is in the same directory as your JS file, you would use:

var mySchema = require('./schema.json')
Richard Walton
  • 4,789
  • 3
  • 38
  • 49
  • Is it standard practice to `require` a JSON file instead of reading & `JSON.parse`ing it? I suppose there could be security implications. – qxz Aug 06 '16 at 22:56
  • What other means would you suggest to get your JSON string into your application to be `JSON.parse`d? `require` statements are evaluated at the build/compilation stage of the application and as such, the developer has complete control over the code / files being executed. – Richard Walton Aug 06 '16 at 23:02
  • Well, `fs.readFile[Sync]`. Is the "build/compilation stage" right when the application launches? You're referring to JIT compilation, right? And you can `require` a file at any time, am I wrong? – qxz Aug 06 '16 at 23:05
  • You're absolutely right. I've too long been in the grunt/browserify world :) Re your original question in the context of a nodejs environment: I'm not sure. A quick google lead to this question: http://stackoverflow.com/questions/35389060/read-json-file-content-with-require-vs-fs-readfile – Richard Walton Aug 06 '16 at 23:09
  • 1
    Good link. Turns out there isn't a security issue; requiring a `.json` file parses it as JSON, not JS. The caching behavior and succinct syntax makes `require` clearly the better choice. [source](http://www.bennadel.com/blog/2908-you-can-use-require-to-load-json-javascript-object-notation-files-in-node-js.htm) – qxz Aug 06 '16 at 23:21