Error: The JSON data contains an unexpected token 'm' in the "module.exp" which is causing a SyntaxError

$ npx json-server --version
1.0.0-alpha.23
$ node -v
v20.12.0
$ ls
generate.js  node_modules  package.json  package-lock.json  public  README.md  src

I've been attempting to launch a React project locally from a GitHub repository. Despite installing most dependencies, I'm facing difficulties executing a .js file using json-server.

The specific file I'm trying to load with json-server is located here: https://github.com/brucevanhorn2/reactstrap-course/blob/master/generate.js

This is the error message appearing in my VS Code terminal after running the command: npx json-server generate.js

undefined:1
module.exports = function(){
^

SyntaxError: Unexpected token 'm', "module.exp"... is not valid JSON
    at JSONFile.parse (<anonymous>)
    at JSONFile.read (file:///home/niveus/PRAKASH/VS_CODE/flying_cars/node_modules/lowdb/lib/adapters/node/DataFile.js:17:31)
    at async Observer.read (file:///home/niveus/PRAKASH/VS_CODE/flying_cars/node_modules/json-server/lib/observer.js:21:22)
    at async Low.read (file:///home/niveus/PRAKASH/VS_CODE/flying_cars/node_modules/lowdb/lib/core/Low.js:16:22)
    at async file:///home/niveus/PRAKASH/VS_CODE/flying_cars/node_modules/json-server/lib/bin.js:114:1

Node.js v20.12.0

I've attempted various export methods like export default () => {} in the generate.js file, yet no success.

Any ideas on what might be causing this issue?

Answer №1

If you're looking to load a JavaScript file for generating random data, keep in mind that the current alpha release of json-server@1 only supports JSON files. This means that trying to use non-JSON content will result in syntax errors. To work around this limitation, you can either generate the data with your JS file and then save it as JSON for use with json-server, or you can opt to install the stable version json-server@0 (currently v0.17.4).

For more information on related topics:

  • Issues encountered when running json-server --watch command
  • Getting "Not found" error in JSON server due to using integers for IDs

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display a specific tab section upon clicking using jQuery or JavaScript

Hello everyone! I am completely new to JavaScript. I have added a tab slider to my HTML with 3 categories in the tab menu: All, Creative, and Branding. How can I show a div after clicking on one of the list items? I have assigned classes to the list items ...

Can anyone recommend which browser compatibility is best suited for my Application?

I'm unsure if this question is relevant, but here goes... When creating an application, should I focus on browser compatibility for IE, Chrome, Firefox, Netscape, Opera, or something else? And if I choose IE, which version should I target - IE6 ...

Accessing JSON child data using PHP (nested within another foreach loop)

I am trying to extract some data from a JSON file where the child element is named "photo" and it contains multiple file names. I need to retrieve these file names within an outer loop. Here is a snippet of my code: This is how my JSON file is structured: ...

Displaying JavaScript - Nothing to Echo in PHP

Using PHP to echo a JavaScript block, I have encountered an error message. echo "<script language='javascript' type='text/javascript'> jQuery(document).ready(function($){ var $lg = $('#mydiv'); ...

Utilize JavaScript, MySQL, and PHP to input data into a database

My JS function is supposed to make an ajax request, but for some reason it's not working. I've checked the URL in an alert and all variables are declared. var request = new XMLHttpRequest(); var url = "ajax_js/q_ajax.php?q="+ques+ ...

Creating a unique Web Component that spans the full height of the page

I created a Web Component with Vue.js and vue-custom-element. I now want my my-chat and my-whiteboard Web Components to have a height of 100%. Here's how I'm using the component: // App.vue <template> <splitpanes class="default-theme" ...

Is it possible to continuously generate webpages using AJAX?

Is there a way to achieve an infinite scrolling effect in all directions using ajax requests without the need for flash or silverlight? If anyone has an example of this, I would love to see it! Thank you for your time. ...

Best practices for persisting nested properties in mongodb using mongoose

Suppose I am looking to create a user schema with nested properties: var userSchema = new mongoose.Schema({ username: { type: String, unique: true, lowercase: true }, /* ... additional properties ... */ profile: { firstName: { type: String, d ...

What is causing the bars to move forward on the x-axis starting from the second one?

I have been working on a chart and here is the code I have so far: plot = $.jqplot('SalesChart2', [ [[1,5]], [[1,10]], [[1,15]], [[1,20]], [[2,25]], ...

Can you explain the significance of the v-on="..." syntax in VueJS?

While browsing, I stumbled upon a Vuetify example showcasing the v-dialog component. The example includes a scoped slot called activator, defined like this: <template v-slot:activator="{ on }"> <v-btn color="red lighten-2" ...

Guide on decoding JSON file generated from curl in PHP

After executing my cURL request, the following code is executed: $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } The respons ...

ng-bind-html is functional, yet it is raising a TypeError

Below is the code snippet containing the ng-bind-html: <span ng-bind-html="text"></span> Here is the stack trace: angular.js:13236 TypeError: bindings.push is not a function at Function.$$addBindingInfo (http://localhost:9000/bower_component ...

How can I incorporate varying textures into an object using `TextureLoader.load`?

I'm interested in adding various textures to each side of a box, but I'm uncertain if using loader.load is the correct approach. Currently, my code looks like this: loader.load('img/brick.jpg', function ( texture ){ var boxGeometry ...

What is the best way to retrieve the root binding node from a viewmodel in order to apply jQuery.blockUI when performing an AJAX post request?

Within my code, I have a designated DIV element that serves as the root node for applying knockout bindings like so: ko.applyBindings(viewModel, document.getElementById('myContainerDiv')); In all of my viewmodel types, there is a generic post m ...

Node.JS guide on handling geonames city information

While unconventional, I wanted to share my solution since there is a lack of information on how to accomplish this task on the stack. After searching for an easy-to-use Node.JS module to process geonames data into a mongo database, I found very few project ...

Encountering a Nuxt error where properties of null are being attempted to be read, specifically the 'addEventListener' property. As a result, both the default

Currently, I am utilizing nuxt.js along with vuesax as my UI framework. I made particular adjustments to my default.vue file located in the /layouts directory by incorporating a basic navbar template example from vuesax. Subsequently, I employed @nuxtjs/ ...

Error Encountered when Using JQuery AJAX: Unexpected Identifier Syntax Issue

I've been struggling with a strange error for quite some time now. I want to believe that this is one of those errors where the solution will magically appear, but only time will tell. Here's the piece of code causing the issue: var images = ...

Implementing Jquery to Repurpose a Function Numerous Times Using a Single Dynamic Value

Important: I have provided a functional example of the page on my website, currently only functioning for the DayZ section. Check it out here Update: Below is the HTML code for the redditHeader click event <div class="redditHeader grey3"> & ...

Creating a comprehensive collection in a single line utilizing jsoncpp's StyledWriter

I've been utilizing jsoncpp library from jsoncpp for handling JSON files. When it comes to writing, I rely on the StyledWriter tool which formats the JSON in a more readable manner. Currently, I am faced with the task of writing an array of integers ...

Updating data from a JSON file using AngularJS: Step-by-step guide

When a user edits a form, the form is updated but I want to retrieve the data from a file called "app.json" instead of from app.js. Below is the code snippet: <!DOCTYPE html> <html> <head> <script data-require="<a ...