1

I am a beginner in learning JavaScript and currently in the process of building a classic game of snakes. I keep pushing my code to git in small changes since that way it becomes easier to track where the issue is happening.

So now I see the below error on the console when i run my game on localhost.

ref2

and on click of the error on console, i get taken to

ref1

There is no syntax error which I am sure so it must be some semantics i am going wrong about here. Any help would be greatly appreciated.

This is my .js file I suspect might be causing the issue

snakes.js

import { getInputDirection } from "./input";

export const SNAKE_SPEED = 1; //Controls the speed of the snake;
const snakeBody = [
  { x: 10, y: 11 },
  { x: 11, y: 11 },
  { x: 12, y: 11 },
];

export function update() {
  const inputDirection = getInputDirection();
  for (let i = snakeBody.length - 2; i >= 0; i--) {
    snakeBody[i + 1] = { ...snakeBody[i] };
  }
  snakeBody[0].x += inputDirection.x;
  snakeBody[0].y += inputDirection.y;
}

export function draw(gameBoard) {
  snakeBody.forEach((segment) => {
    const snakeElement = document.createElement("div");
    snakeElement.style.gridRowStart = segment.y;
    snakeElement.style.gridColumnStart = segment.x;
    snakeElement.classList.add("snake");
    gameBoard.appendChild(snakeElement);
  });
}

input.js

let inputDirection = { x: 0, y: 0 };

export function getInputDirection() {
  return inputDirection;
}

1 Answer 1

2

The error here is simple. You have an error with importing your input.js in snakes.js here.

Change

import { getInputDirection } from "./input";

To

import { getInputDirection } from "./input.js";
Sign up to request clarification or add additional context in comments.

2 Comments

Okay so we need to include the .js extension?
Yeah that's why you are seeing the error. Try it and check back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.