title: Mocha + TypeScript: Cannot use import statement outside a module
body: <p>I was watching <a href="https://www.youtube.com/watch?v=I4BZQr-5mBY" rel="noreferrer">this video</a> in order to learn how to add some simple tests to my Express routes but I am getting all kind of errors while executing a test. The error is:</p>
  
  <blockquote>
    <p>import * as chai from 'chai';</p>
    
    <p>^^^^^^</p>
    
    <p>SyntaxError: Cannot use import statement outside a module</p>
  </blockquote>
  
  <p>I have read some similar Stack Overflow questions and GitHub issues but I didn't find a solution for my own application. Finally I found <a href="https://github.com/mochajs/mocha-examples/tree/master/packages/typescript#es-modules" rel="noreferrer">Mocha documentation</a> on GitHub regarding ES modules but it didn't work:</p>
  
  <p>I created the app using TypeScript and CommonJS module to transpile, so I added <code>"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha src/test/*.ts"</code> to the <code>package.json</code> scripts but I am getting the same error every time. I am using <code>ts-node</code> as a server.</p>
  
  <p>Anyway, this is my <code>tsconfig.json</code> file:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
      "compilerOptions": {
          "sourceMap": true,
          "target": "es6",
          "module": "commonjs",
          "outDir": "./dist",
          "rootDir": "./src"
      },
      "exclude": [
          "node_modules"
      ]
  }
  </code></pre>
  
  <p>And this is the <code>src/test/mi-route.ts</code> file:</p>
  
  <pre class="lang-js prettyprint-override"><code>import * as chai from 'chai';
  import * as chaiHttp from 'chai-http';
  import server from '../app';
  
  // Assertions
  chai.should();
  
  chai.use(chaiHttp);
  
  describe('API Users', () =&gt; {
      // Test Route GET
      describe('GET /api/admin/users', () =&gt; {
          it('Should return all the users', done =&gt; {
              chai.request(server)
                  .get('/api/admin/users')
                  .end((err, response) =&gt; {
                      response.should.have.status(200);
                      response.body.should.be.a('object');
                      done();
                  });
          });
      });
  });
  </code></pre>
  
  <p>An this is my <code>package.json</code> scripts:</p>
  
  <pre><code>"scripts": {
      "dev": "ts-node-dev src/app.ts",
      "start": "node dist/app.js",
      "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha src/test/*.ts",
      "build": "tsc -p"
    },
  </code></pre>
  
  <p>So... any suggestions? Should I change Common JS Module? Thanks in advance</p>