Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected token 'export' #37

Closed
J9B10 opened this issue Jun 16, 2023 · 3 comments
Closed

SyntaxError: Unexpected token 'export' #37

J9B10 opened this issue Jun 16, 2023 · 3 comments

Comments

@J9B10
Copy link

J9B10 commented Jun 16, 2023

Hello

I have a script that makes many axios requests and it causes errors like AxiosError: getaddrinfo EAI_AGAIN due to many DNS requests.

Then I saw on the internet that I could use this axios-cached-dns-resolve module to cache DNS in axios.

But whenever I implement the code and run my script it returns the error:

\axios-cached-dns-resolve\index.js:1
export {
^^^^^^

SyntaxError: Unexpected token 'export'

I do not understand where is the fault.

It looks like the official help page is confusing with import examples.
My script does not accept importation because it is a single script like myscript.js where I put all the code to be executed.

In myscript.js

I avoided using the //import as my execution returns the error:

import { registerInterceptor } from 'axios-cached-dns-resolve'
^^^^^^

SyntaxError: Cannot use import statement outside a module

Instead of import I used const:
const { registerInterceptor } = require("axios-cached-dns-resolve");

Still I get the error:

\axios-cached-dns-resolve\index.js:1
export {
^^^^^^

SyntaxError: Unexpected token 'export'

It seems to me that the module is broken or I am doing something wrong.

I need help.

My example code: myscript.js

//import { registerInterceptor } from 'axios-cached-dns-resolve'

const { registerInterceptor } = require("axios-cached-dns-resolve");

const axiosClient = axios.create(config);

function func_01(a, b, c) {
	url = `https://domain.com`;
	//axios
	registerInterceptor(axiosClient)		
		.get(url)
	// ... mais código aqui ... //
	}
  • Note: This is just an abbreviated piece of code.
    Because it's fine and it's functional and there's no need to post all the code.
  • The only problem and the implementation of axios-cached-dns-resolve.
  • All necessary dependencies are in node_modules.

Please someone help me to solve this.

@tcollinsworth
Copy link
Owner

tcollinsworth commented Jun 16, 2023 via email

@J9B10
Copy link
Author

J9B10 commented Jul 9, 2023

I have identified the problem.

Actually I needed to convert all my script to ESM.

I had difficulty converting due to lack of information, but through a lot of research I was able to convert to ESM.

I put the "type": "module", in the package.json

and then I converted all exports and imports following https://reflectoring.io/nodejs-modules-imports/.

Now my script is a Nodejs ESM.

@tcollinsworth
Copy link
Owner

Glad you got it working.

That was one way and probably the best because many libraries are going native ESM. This library isn't native esm yet, so you could have just loaded the esm or esm-wallaby library (wallaby if I used optional chaining) before loading this library and it would have worked without changing the type or renaming all files from .js to .mjs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants