Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  169] [ 7]  / answers: 1 / hits: 29747  / 5 Years ago, sun, january 27, 2019, 12:00:00

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?


More From » node.js

 Answers
22

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


[#52705] Wednesday, January 23, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;