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
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).
You try this. Hope it helps
const 'your_variable' = require('your_required_module or file_path')
In your case
const dict = require( './words_dictionary')
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