3

I can't import anything in my javascript files of my website, I've tried many things but it doesn't work.

So I want to import a .json file to a .js file using the import key word but it doesn't work. Another problem is that Visual Studio Code doesn't show me an error but the console does...

main.js import part

import dropDownList from "./navigationList.json"; //This is line number 1

console.log(dropDownList.shop.kits);

navigationList.json file

{
    "shop": {
        "kits": "DIY Physics Kits",
        "merch": "Merchendise"
    },

    "learn": {
        "first-sub": "Mechanics",
        "second-sub": "Miscellaneous"
    }
}

Error the console is showing:

mainJS.js:1 Uncaught SyntaxError: Unexpected identifier

The console says there is an Uncaught SyntaxError with my import and this causing me to make my code long and without import which sucks. Please help me

3
  • 1
    import keyword doesnt work with importing json data, it can only import defined modules from js file. See documentation - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… and workaround - stackoverflow.com/questions/34944099/… Commented May 28, 2019 at 18:46
  • 1
    use the fetch api to get your JSON. Commented May 28, 2019 at 18:48
  • @Sarwara - this: import keyword doesn't work with importing json data is not accurate. One can import JSON data as long as it is exported. But, one cannot import from a MIME type other than JS (i.e navigationList.json) Commented May 28, 2019 at 19:05

4 Answers 4

3

The import syntax is part of ES6 modules. It looks like you are using Node.js, which does not include support for ES6 modules by default. However, Node.js has experimental support for ES6 modules.

  1. Rename the file to have an .mjs extension:
mv mainJS.js mainJS.mjs
  1. Run it with the --experimental-modules flag:
node --experimental-modules mainJS.mjs

I tested this with your code, and it works:

% node --experimental-modules main.mjs
(node:73044) ExperimentalWarning: The ESM module loader is experimental.
DIY Physics Kits

At the time of this writing, I am using Node.js v12.3.1:

% node --version
v12.3.1

Alternatively, you can transpile your code with a tool like Babel, which changes imports to requires.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Elliot you really helped me altough I am not using Node.js, this scripts are connected to my web page with the <script> tag, However I will try to transpile my code in order to use require() and I will see if it works. Note: Currently when I am trying to use the word require() in my code the console says that require is not defined.
By the way: This article says that without a server I can't use the import keyword, and currently I am running my website on my computer with the protocol file:// so I guess that was the main problem...
1

navigation_list.json

{
    "shop": {
        "kits": "DIY Physics Kits",
        "merch": "Merchendise"
    },

    "learn": {
        "first-sub": "Mechanics",
        "second-sub": "Miscellaneous"
    }
}

main.js

var navigation_list = require('./navigation_list.json');
console.log(`>>>> Returned JSON data: ${navigation_list.shop.kits}`);

End the output here:

Comments

0

Set "type": "commonjs" in package.json

Comments

0

This usually happens when we copy paste some components from other directory into our project. I got this working by saving the components one by one once pasted and immediately I was able to see them getting auto imported without installing any extensions

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.