(function (exports, require, module, __filename, __dirname) { import "mocha";
^^^^^^
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 C:\Users\pendyar\Desktop\napi\Design\OpenAPI\napi-ec2\node_modules\mocha\lib\mocha.js:231:27
at Array.forEach (
at Mocha.loadFiles (C:\Users\pendyar\Desktop\napi\Design\OpenAPI\napi-ec2\node_modules\mocha\lib\mocha.js:228:14)
at Mocha.run (C:\Users\pendyar\Desktop\napi\Design\OpenAPI\napi-ec2\node_modules\mocha\lib\mocha.js:536:10)
at Object.
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 Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
Some one help me with suggestions on how to resolve this error
I don't ever use import when I run Mocha tests, so not sure what your test script looks like. https://mochajs.org/#getting-started
import is an ESNext feature that is not available yet in any Node.js runtime. If you use the import keyword you must transpile your code before running it.
On the other hand, if you change your import statements to require() calls, you won't need to transpile.
http://2ality.com/2017/09/native-esm-node.html
https://medium.com/@giltayar/native-es-modules-in-nodejs-status-and-future-directions-part-i-ee5ea3001f71
hey @Revanth453. You are using the word import, which is not supported by the node environment you are running. To use features that are not supported yet you have to transpile the code before you run it as @ORESoftware mentions.
To run the babel transpiler with mocha for example, use babel-register as a first argument in the require mocha option.
# mocha.opts
--require babel-register ./test/setup
test/**/*.test.js
I am closing this since it is not related to mocha per se. Feel free to ask on gitter if you feel you need more help. Good luck :)
I am using ts-node and PHPStorm/Mocha. In the Mocha configuration dialog, there is a field "Extra Mocha options". I entered --require ts-node/register there and it resolved this issue.
/usr/local/lib/node_modules/mocha/lib/esm-utils.js:6
return import(url.pathToFileURL(file));
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/cli/run-helpers.js:18:27)
I am getting this error while running test by mocha test --reporter mocha-junit-reporter .
Is anything wrong I am doing here?
/usr/local/lib/node_modules/mocha/lib/esm-utils.js:6 return import(url.pathToFileURL(file)); ^^^^^^ SyntaxError: Unexpected token import at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/cli/run-helpers.js:18:27)I am getting this error while running test by
mocha test --reporter mocha-junit-reporter.
Is anything wrong I am doing here?
Apparently the latest mocha version is broken. I installed 7.1.1 which solved the error above.
/usr/local/lib/node_modules/mocha/lib/esm-utils.js:6 return import(url.pathToFileURL(file)); ^^^^^^ SyntaxError: Unexpected token import at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/cli/run-helpers.js:18:27)I am getting this error while running test by
mocha test --reporter mocha-junit-reporter.
Is anything wrong I am doing here?Apparently the latest mocha version is broken. I installed 7.1.1 which solved the error above.
I upgraded my mocha to 8.1.0.
I am still facing the same issue. Is there any update on how can I fix this. I need mocha 8 and above to run parallel tests.
@rohitpiush @bramevo @exrishu Could one of you open a new bug report on this instead of piggybacking on an already closed issue that is unrelated? There is no guarantee that any maintainer will see this, as we might be unsubscribed.
Please take the time to fill in the important details in the bug report, as specified in the bug report template, so we have a chance to recreate the problem.
/usr/local/lib/node_modules/mocha/lib/esm-utils.js:6 return import(url.pathToFileURL(file)); ^^^^^^ SyntaxError: Unexpected token import at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/cli/run-helpers.js:18:27)I am getting this error while running test by
mocha test --reporter mocha-junit-reporter.
Is anything wrong I am doing here?
I had the same problem. I found a solution here (https://github.com/mochajs/mocha/issues/4377)
/usr/local/lib/node_modules/mocha/lib/esm-utils.js:6 return import(url.pathToFileURL(file)); ^^^^^^ SyntaxError: Unexpected token import at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/cli/run-helpers.js:18:27)I am getting this error while running test by
mocha test --reporter mocha-junit-reporter.
Is anything wrong I am doing here?I had the same problem. I found a solution here (#4377)
But I don't want to change node modules file. Is there any other way?
Same here, downgrading to v7 ("mocha": "^7.1.1",) has helped, but it is not a permanent solution,..
@webia1 @rohitpiush you keep commenting on an issue that is unrelated to your problem. If you want your separate issue fixed you need to give us relevant information to work with. Open a new issue and fill out the issue template so we have a chance to recreate those problem. Otherwise nothing will get fixed and you and others will keep having this problem
Most helpful comment
import is an ESNext feature that is not available yet in any Node.js runtime. If you use the import keyword you must transpile your code before running it.
On the other hand, if you change your import statements to require() calls, you won't need to transpile.
http://2ality.com/2017/09/native-esm-node.html
https://medium.com/@giltayar/native-es-modules-in-nodejs-status-and-future-directions-part-i-ee5ea3001f71