On Node 13.8 I'm trying to use import / export.
EG:
import {ChatClient, Message, MessageParser} from './chat-client/module.js';
But when i do this, I get
SyntaxError: Cannot use import statement outside a module
So in my package.json I set "type" : "module"
but now when I try to use const io = require('socket.io-client');
I get ReferenceError: require is not defined
Is there a way to use import / export AND require?
The original error, does that just mean I have to wrap my library in a NPM library? It's a Library that's used both front end and backend so using import / export is important.
Thanks
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With