2

I am trying to import 'setCORS' from a node module 'google-translate-api-browser' but i am getting the error SyntaxError: Unexpected token {

I already tried specifying type="module" in my main script file but it seems to have no effect.

import { setCORS } from "google-translate-api-browser";
//setting up cors-anywhere server address
const translate = setCORS("http://cors-anywhere.herokuapp.com/");

translate(original.textContent(), {to:'ne'})
  .then(res => {
  console.log(res.text);
})
.catch(err => {
  console.log(err);
});
6
  • Importing npm modules this way is only allowed in NodeJS, if you want to import modules from the web you need to use a relative path instead of the module name. Commented Aug 25, 2019 at 17:42
  • it's showing the same error even if i run it on a node server. Commented Aug 25, 2019 at 17:52
  • have you installed the package? Commented Aug 25, 2019 at 17:53
  • yes i installed it. but should i mention it on the package.json file too? Commented Aug 25, 2019 at 17:55
  • no that shouldn't be causing any issues at this stage. you mentioned you tried specifying type="module". That is only a thing in HTML script imports, which node doesn't use. How are you running the code? Commented Aug 25, 2019 at 17:57

1 Answer 1

1

Looks like you installed the module on an incorrect path..... In the command line try (make sure youre in the main parent directory):

1.rm -rf node-modules
2. npm install --save google-translate-api-browser
3. npm install 
4. import  the module: import {setCors} from "google-translate-api-browser"
5. Test it out
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.