Import not working on Node.js version 11.8.0

20,585

Solution 1

Have you been able to use the import keyboard elsewhere in your code? The issue here may be that you aren't transpiling your code into ECMAScript 5. Since import is an ECMAScript 6 feature, it hasn't yet been fully supported by Node.js. If you use a tool like Babel to transpile your code, you may fix this issue. If you don't want to do this, try using require instead.

As noted, in Node.js 9+ you can also use it in .mjs files with the --experimental-modules flag enabled.

node --experimental-modules file.mjs

Node.js import compatibility

Solution 2

It's only supported with an experimental flag. You should use the --experimental-modules flag.

Or just use require simple as that or if you really want, you can transpile your code with browserify, babel or parcel or whatever.

I think this should work if you run code like this:

node --experimental-modules index.mjs

Note that it uses the mjs extension (modular JavaScript I think).

Solution 3

You try this. Hope it helps

const 'your_variable' = require('your_required_module or file_path')

In your case

const  dict = require( './words_dictionary')
Share:
20,585
MilesZew
Author by

MilesZew

I am a young, intermediate level programmer.

Updated on July 09, 2022

Comments

  • MilesZew
    MilesZew almost 2 years

    I am writing a simple program that uses a object full of dictionary words. I want to import that object from a different file as it is very large. When trying to import it I get an error that looks like Node.js doesn't know what it is.

    I have already tried reinstalling the latest version of Node.js.

    Here is the important code:

    import {dict} from './words_dictionary'
    

    And here is all of it:

    import {dict} from './words_dictionary'
    
    
    function exists(obj,str) {
      if(obj[str]) {
        return true
      } else {
        return false
      }
    }
    console.log(exists(dict, 'hello'))
    

    Here is the gist of the dictionary code:

    export let dict = {a: 1, aa: 1, aaa: 1, aah: 1, aahed: 1, aahing: 1, aahs:
    1, aal: 1, aalii: 1, aaliis: 1, aals: 1, aam: 1, aani: 1, aardvark: 1,
    aardvarks: 1,...~3000 more}
    

    I expected true, but I got this error:

    SyntaxError: Unexpected token {
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:696:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:568:12)
    at Function.Module._load (internal/modules/cjs/loader.js:560:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
    at executeUserCode (internal/bootstrap/node.js:526:15)
    

    ECMAScript 6 is now working, but I am now getting the error of dict not being defined. Could this have something to do with the file size, because I have checked multiple times for spelling errors?