M.Hassan Nasir
M.Hassan Nasir

Reputation: 861

Can I access a variable I declared in a js file in different js file

I'm trying to export this js file.

const student =
{
    studentID : 1,
}
export {student}

this is my second file in which I'm trying to import it.

import {student} from "./studentId";
console.log(student);

It's not importing it and throwing this error.

Failed to load resource: the server responded with a status of 404 ()

Upvotes: 1

Views: 56

Answers (1)

Quentin
Quentin

Reputation: 944530

Look at the error message. You are getting a 404 Not Found error.

Node.js does not need a file extension when loading a module. It has access to the file system and can look at all the files in a directory.

Web browsers do not have any way of telling if the URL you specified should have a .js or .jsx or .mjs or .tsx or whatever extension added to it.

You need to specify the actual URL you are importing.

 import {student} from "./studentId.js";

Upvotes: 1

Related Questions