SyntaxError: Cannot use import statement outside a module - nodejs - javascript

I'm using a JS library wrapper for guerrilla mail api. It's written for ES6 so the it use the import to load dependencies. Since I need to require it inside a cli node script, I get this error when I try to require it:
/Users/dev/Desktop/guerrillamail-cli/node_modules/guerrillamail-api/src/index.js:1
import axios from 'axios';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Is there any way to use the library? I'm thinking to replace all the import with require but not sure if this exploit will work. Any suggestion about?

Related

How to use winston error handling with express-async-errors using ES6 syntax?

I'm using ES6 in Node.js and want to handle uncaughtException and unhandledRejection using express-async-errors.
Documentation say that I should only require module, but what should I do with ES6?
// require('express-async-errors');
import expressAsyncErrors from 'express-async-errors';
Actually I want to handle errors using winston and with express-async-errors I want to handle uncought and unhandled errors.
So how can I use it?
express-async-errors patches express package when it's imported.
In case it's imported like
import expressAsyncErrors from 'express-async-errors';
and expressAsyncErrors isn't used, unused import can be dropped, depending on ES module implementation. In order to just import the package, it should be:
import 'express-async-errors';

require an es6 module into an es5 module

This is a strange issue, but I'm trying to require an es6 module into a section of my application that is using es5.
es6file.js
import moment from 'moment'
etc...
es5file.js
var config = require('../../../es6file')
etc...
and I keep getting an error of Unexpected token import
This, of course, makes sense. But I'm wondering if there is an easy way to allow myself to import this file without updating my entire application to handle imports instead of requires.

React, Getting error: Uncaught ReferenceError: require is not defined

I have a project where I use both react and node (express). When I link to react using src="https://unpkg.com/react#16/umd/react.development.js" etc.. I have no problem using react with JSX in the project, but when I try to import like this:
import React from "react";
I get the error: Uncaught ReferenceError: require is not defined
This kind of "handicaps" me since I want to use modules like axios etc..
I am not using any module bundler
Thanks for any help!
Don't know if I fully understand the question but you seem to be using React.js from a cdn link. With no module bundler. So why are you importing it also? You've already "imported" React.js from your cdn link. =) No need to import it again. The import statement is when you use modules and import them like that with the ES6 syntax. If you want to do that use create-react-app instead.
I had a similar issue that I eventually solved with Browserify. Here's a link to my answer:
https://stackoverflow.com/a/63356350/5132452

Module not found when using import instead of require

I am trying to use import instead of require for all modules in my project, but some older npm modules only have instructions for require.
In the case of 'isomorphic-fetch' I can't find the proper way to use import:
works
require('isomorphic-fetch')
fails
import 'isomporphic-fetch'
import Something from 'isomorphic-fetch'
// error Can't resolve 'isomporphic-fetch' from Project/src/js/
Converting to import does work with the es6-promise module.
works
require('es6-promise').polyfill()
works
import Something from 'es6-promise'
Something.polyfill()
Since import does work with other modules, and require('isomorphic-fetch') works, it's probably a named export problem.
Try import * as Something from 'isomorphic-fetch'
If that works, it's because isomorphic-fetch does not do export deafult so you have to pull in the imports by name, or use the notation I wrote above. Take a look at the MDN link I put on top.

How to use ES6 import with hyphen

I really don't know how to do this and not sure how to google either.
Right now I have this
let source = require('vinyl-source-stream');
I would like to change to be import but this doesn't work
import {'vinyl-source-stream' as source} from 'vinyl-source-stream';
If that module even supports the ES6 import/export system, then what you want is this:
import source from 'vinyl-source-stream';
Your version is attempting to import an exported value named vinyl-source-stream from the module; instead, you just want the module itself to be imported (into an object named source in this case).
If you want everything in the module imported, instead of just the default exports, use this instead:
import * as source from 'vinyl-source-stream';
But neither of those will work if the module isn't actually written to use the new system.
This library doesn't use the ES2015 module system. It doesn't export at all, so you can't import it or from it.
This library uses the CommonJS module pattern (as can be seen in the source) and is meant to be requireed.
You could import the library with:
import form 'vinyl-source-stream';
which will cause the code to be executed, but that will be useless in this case since nothing (useful) will happen - in fact, you'll probably get a runtime exception due to undefined module.

Categories