Import not working on Node.js version 11.8.0

后端 未结 3 1727
刺人心
刺人心 2021-01-06 05:24

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

相关标签:
3条回答
  • 2021-01-06 05:44

    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).

    0 讨论(0)
  • 2021-01-06 05:48

    You try this. Hope it helps

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

    In your case

    const  dict = require( './words_dictionary')
    
    0 讨论(0)
  • 2021-01-06 06:01

    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

    0 讨论(0)
提交回复
热议问题