question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throws ERROR Unexpected token import

See original GitHub issue

Hi, I am trying the example given in node-casbin readme and facing following error.

error: uncaughtException: Unexpected token import
/server/services/casbin.service.js:1
(function (exports, require, module, __filename, __dirname) { import * as casbin from 'casbin';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/server/routers/casbin.routes.js:3:21)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/server/routers/app.routes.js:95:24)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
HarshadMohape0904commented, Sep 26, 2019

@HarshadMohape0904 The await method is not working. Please use the following function.

casbin.newEnforcer("../path/model.conf", "../path/policy.csv").then(function (enforcer) {
          console.log("success =  ", enforcer)
}).catch(function (error) {
            console.log("error =  ", error)
})

Thanks, @akshay27395 I tried it and it worked…!

1reaction
HarshadMohape0904commented, Sep 26, 2019

I tried the above solution but it leads me to a new error as below.

var test = await casbin.newEnforcer("../path/model.conf", "../path/policy.csv").then(function (enforcer) {
                         ^^^^^^

SyntaxError: Unexpected identifier
   at createScript (vm.js:80:10)
   at Object.runInThisContext (vm.js:139:10)
   at Module._compile (module.js:607:28)
   at Object.Module._extensions..js (module.js:654:10)
   at Module.load (module.js:556:32)
   at tryModuleLoad (module.js:499:12)
   at Function.Module._load (module.js:491:3)
   at Function.Module.runMain (module.js:684:10)
   at startup (bootstrap_node.js:187:16)
   at bootstrap_node.js:608:3
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js - SyntaxError: Unexpected token import - Stack Overflow
Use transpiler like Babel to use import in Nodejs as it is not natively supported in nodejs.There is best alternative of import is...
Read more >
SyntaxError: Unexpected token import in Node.js | bobbyhadz
The "SyntaxError: Unexpected token import" occurs when we use the ES6 import syntax in a version of Node that doesn't support it. To...
Read more >
Error Unexpected token import in nodejs | Edureka Community
SyntaxError : Unexpected token import at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:387:25) at Object.Module.
Read more >
Unexpected token import when importing a module written ...
Issue description or question When importing a module written with es6 imports, I get the error message Unexpected token import Quokka.js ...
Read more >
Unexpected token import Node.js - Reactgo
To fix this error, we can use the commonjs modules require() function and module.exports instead of es6 modules. app.js. const express = require ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found