Ts-Node Cannot use import statement outside a module with Mocha

(hey why is this editor so crappy, why is it all grey text every time I post a topic?  It's been like this for years)

So I'm able to run this just fine from the command-line (from root of the project):

yarn --cwd src/graphql test-graphql-integration

Here is the script: 

"test-graphql-integration": "NODE_ENV=test yarn mocha --reporter min test/integration/**/*.spec.ts -r test/compiler.ts -t 6000",

compiler.ts is a test helper file which applies ts-node to my test runs:

project: "test/tsconfig.testing.json",

So my folder structure is this

> src
   > graphql
       > test
           > integration
                 // all the .spec.ts files are in here


Ok so the problem I have now is I used to be able to run these tests just fine with the JetBrains test runner.  But I just cannot get past this error when I'm running it from the JetBrains test runner. 

It's weird because why can I run this from the command-line knowing it is hitting the same compiler.ts and tsconfig.testing.json but errors on imports?


"ts-node": {
"files": true,
"transpileOnly": true
"compilerOptions": {
"noEmit": true,
"sourceMap": false,
"module": "esnext",
"moduleResolution": "node",
"target": "es6",
// "lib": ["esnext"],
"inlineSources": false,
"strictNullChecks": false,
"noImplicitAny": false,
"noImplicitThis": false,
"esModuleInterop": true,
"skipLibCheck": true
"include": ["./**/*.spec.ts"],
"exclude": [


"name": "we-do-tdd-graphql",
"engines": {
"node": "14.x",
"npm": "6.x"
"scripts": {
"test-graphql-integration": "NODE_ENV=test yarn mocha --reporter min test/integration/**/*.spec.ts -r test/compiler.ts -t 6000",
"dependencies": {
"@babel/register": "^7.10.5",
"@babel/runtime": "^7.10.5",
"@types/express": "^4.17.7",
"@types/express-graphql": "^0.9.0",
"express": "^4.17.1",
"express-graphql": "^0.11.0",
"devDependencies": {
"@types/chai": "^4.2.18",
"@types/mocha": "^8.2.2",
"@types/node": "^15.6.1",
"chai": "^4.3.4",
"mocha": "^8.4.0",
"ts-node": "^10.0.0",
"typescript": "^4.2.4"

Can't make the tests work when using similar setup, even when running your script in terminal. Do you have .babelrc in your project, what does it look like, what folder is it located in? A complete sample project the issue can be repeated with would be helpful


Yes there's a .babelrc.  Again the tests run fine from the command-line (no idea why you aren't able to), but it's just not working anymore from WebStorm's test runner anymore.  I upgraded latest so I can't tell if it's a WebStorm or some other issue.

I also BTW tried the same test config in IntelliJ, no go, same error.

I can't share the project publicly but I can add you as a collaborator temporarily so that you can run it.  Can you let me know what your github id is so I can add as a collaborator and I'll do that ASAP.  The repo is https://github.com/dschinkel/we-do-tdd so it's private.


Not sure what you mean, that's not a github id...I can't add you to my repo with 53188347


It's an ID as reported by https://api.github.com/users/

Do you need a user name and not an ID?


Yes I need your username to be able to add you as a collaborator to my github repo


Elena, you're added.  Please check out the build2 branch.  I won't push anything else up there and give you a chance to play with it.  Let me know if you have any questions.


Thank you!

For me, results are the same in both the IDE and terminal:


Note that I had to change the script to

"test-graphql-integration": "SET NODE_ENV=test && yarn mocha --reporter min test/integration/**/*.spec.ts -r test/compiler.ts -t 6000",

as I'm on Windows... Am I doing anything wrong?



I figured this out.  I'm going to write a blog post about this. I'll paste it in when I'm done writing it.


Hi Dave Schinkel

I'm having the same kind of issue, just after updating Webstorm. Did you have any chance to describe your fix in a blog post?


we are having the same issue. Here is the users.test.ts , we can run from command line but not from webstorm 2021.3.1 

// import assert from 'assert';
import app from '../../src/app';
import * as assert from 'assert';

describe('\'users\' service', () => {
it('registered the service', () => {
const service = app.service('users');

assert.ok(service, 'Registered the service');

//package.json (command line works)

"scripts": {
"test": "npm run lint && npm run compile && npm run mocha",
"lint": "eslint src/. test/. --config .eslintrc.json --ext .ts --fix",
"dev": "ts-node-dev --no-notify src/",
"start": "npm run compile && node lib/",
"mocha": "mocha --require ts-node/register --require source-map-support/register \"test/**/*.ts\" --recursive --exit",
"compile": "shx rm -rf lib/ && tsc"

The error is:

usr/bin/node /home/reda/dev/js/featherjs/feathers-cli-211224/node_modules/mocha/bin/mocha --ui bdd --reporter /home/reda/.local/share/JetBrains/Toolbox/apps/WebStorm/ch-0/213.6461.19/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js /home/reda/dev/js/featherjs/feathers-cli-211224/test/services/users.test.ts --grep ^'users' service


import app from '../../src/app';

SyntaxError: Cannot use import statement outside a module


to make it work in Webstorm, I have to edit the configuration and pass the following arguments to mocha.

--require ts-node/register --require source-map-support/register

Please sign in to leave a comment.