javascript - ReferenceError: Can't find variable: require - JQuery error -
i use code in .js file part of web site used via npm global http-server:
function getparsedstorage() { let fs = require('fs'); return json.parse(fs.readfilesync('./../manager-data.json', 'utf8')); }
and errors:
jquery.deferred exception: can't find variable: require (2) (exceptionhook — jquery.min.js:2:31584)
referenceerror: can't find variable: require (jquery.min.js:2:31697)
i've installed file-system package npm install file-system --save didn't change anything.
it looks me you're attempting make use of node.js's require
function within web browser without using module system provides compatible require
method.
without additional information can't certain, advice investigate module loader require.js, systemjs or browserify should enable make use of syntax in module.
that being said, use case you're describing (accessing json file website) may better served using simple xhrhttprequest or fetch
api.
the following example makes use of fetch
api return parsed json file content.
// returns promise resolves parsed storage data // note: lacks error handling function getparsedstorage() { return fetch("../manager-data.json").then(response => response.json()) }
Comments
Post a Comment