NodeJS (Server): ReferenceError: require is not defined when type: module

前端 未结 2 1977
既然无缘
既然无缘 2021-01-18 12:08

On Node 13.8 I\'m trying to use import / export.

EG: import {ChatClient, Message, MessageParser} from \'./chat-client/module.js\';

But when i d

相关标签:
2条回答
  • 2021-01-18 12:37

    As the documentation says:

    No require, exports, module.exports, __filename, __dirname
    These CommonJS variables are not available in ES modules.
    

    https://nodejs.org/api/esm.html#esm_no_require_exports_module_exports_filename_dirname

    You can't use both natively. If you want to do that use Babel to transpile your code.

    0 讨论(0)
  • 2021-01-18 12:55

    Several points:

    1. Use .mjs instead of .js.
    2. Never use require.
    3. It is optional to use "type": "module".
    0 讨论(0)
提交回复
热议问题