title: How to import or require a TS-module into CommonJS file?
body: <p>For whatever reason, some of my modules have to be <code>js</code> files.</p>
  <p>I <code>import</code> ts module into my <code>js</code> file like this:</p>
  <p><code>resolver.js</code>:</p>
  <pre><code>import { Message } from '../../connectors/message';
  
  // console.log('Message: ', Message);
  
  const resolver = {
    Query: {
      sendMessage: (root, args, ctx) =&gt; {
        console.log(args, ctx);
        return Message.send('1', 'message');
      }
    }
  };
  
  export { resolver };
  </code></pre>
  <p><code>message.ts</code>:</p>
  <pre><code>const Message = {
    send(id, type) {
      return Promise.resolve({
        recipient_id: '123',
        message_id: '122'
      });
    }
  };
  
  export { Message };
  </code></pre>
  <p>I use <code>webpack</code> to and <code>awesome-typescript-loader</code> to compile my <code>.ts</code> files.</p>
  <p>Here is my <code>tsconfig.json</code>:</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      &quot;target&quot;: &quot;es6&quot;,
      &quot;lib&quot;: [&quot;es6&quot;, &quot;esnext&quot;],
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;moduleResolution&quot;: &quot;node&quot;,
      &quot;sourceMap&quot;: true,
      &quot;experimentalDecorators&quot;: true,
      &quot;emitDecoratorMetadata&quot;: true,
      &quot;declaration&quot;: false,
      &quot;outDir&quot;: &quot;dist&quot;,
      &quot;typeRoots&quot;: [&quot;node_modules/@types&quot;]
    },
    &quot;files&quot;: [&quot;src/main.ts&quot;],
    &quot;exclude&quot;: [&quot;node_modules&quot;]
  }
  </code></pre>
  <p><code>webpack.config.js</code>:</p>
  <pre><code>var nodeExternals = require('webpack-node-externals');
  var webpack = require('webpack');
  var path = require('path');
  var fs = require('fs');
  
  /* helper function to get into build directory */
  var distPath = function(name) {
    if (undefined === name) {
      return path.join('dist');
    }
  
    return path.join('dist', name);
  };
  
  var webpack_opts = {
    entry: './src/main.ts',
    target: 'node',
    output: {
      filename: distPath('main.js'),
      libraryTarget: 'commonjs2'
    },
    resolve: {
      extensions: ['.ts', '.js'],
      modules: ['node_modules', 'src']
    },
    plugins: [
      new webpack.LoaderOptionsPlugin({
        options: {
          test: /\.ts$/,
          ts: {
            compiler: 'typescript',
            configFileName: 'tsconfig.json'
          },
          tslint: {
            emitErrors: true,
            failOnHint: true
          }
        }
      })
    ],
    devtool: 'source-map',
    module: {
      loaders: [
        {
          test: /\.ts$/,
          loaders: 'awesome-typescript-loader'
        }
      ]
    },
    externals: [nodeExternals()]
  };
  
  module.exports = webpack_opts;
  </code></pre>
  <p>But I got this error:</p>
  <pre><code>(function (exports, require, module, __filename, __dirname) { import { Message } from '../../connectors/message';
                                                                ^^^^^^
  
  SyntaxError: Unexpected token import
      at new Script (vm.js:51:7)
      at createScript (vm.js:136:10)
      at Object.runInThisContext (vm.js:197:10)
      at Module._compile (internal/modules/cjs/loader.js:618:28)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
      at Module.load (internal/modules/cjs/loader.js:566:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
      at Function.Module._load (internal/modules/cjs/loader.js:498:3)
      at Module.require (internal/modules/cjs/loader.js:598:17)
      at require (internal/modules/cjs/helpers.js:11:18)
  </code></pre>
  <p>How to solve this?</p>
  <p>-- update v1 --</p>
  <p>I try to use it with <code>require</code></p>
  <pre><code>// import { Message } from '../../connectors/message';
  
  const { Message } = require('../../connectors/message');
  
  console.log('Message: ', Message);
  
  const resolver = {
    Query: {
      sendMessage: (root, args, ctx) =&gt; {
        console.log(args, ctx);
        return Message.send('1', 'message');
      }
    }
  };
  
  export { resolver };
  </code></pre>
  <p>But I still got an error:</p>
  <pre><code>internal/modules/cjs/loader.js:550
      throw err;
      ^
  
  Error: Cannot find module '../../connectors/message'
      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:548:15)
      at Function.Module._load (internal/modules/cjs/loader.js:475:25)
      at Module.require (internal/modules/cjs/loader.js:598:17)
      at require (internal/modules/cjs/helpers.js:11:18)
      at Object.&lt;anonymous&gt; (/Users/ldu020/workspace/workplace-adapter-graphql/src/graphql/modules/messages/resolver.js:3:21)
      at Module._compile (internal/modules/cjs/loader.js:654:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
      at Module.load (internal/modules/cjs/loader.js:566:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
      at Function.Module._load (internal/modules/cjs/loader.js:498:3)
  </code></pre>
  <p>-- update v2 --</p>
  <p>I add <code>.ts</code> file extension to path:</p>
  <pre><code>const { Message } = require('../../connectors/message.ts');
  </code></pre>
  <p>still got error:</p>
  <pre><code>    export { Message };
  ^^^^^^
  
  SyntaxError: Unexpected token export
      at new Script (vm.js:51:7)
      at createScript (vm.js:136:10)
      at Object.runInThisContext (vm.js:197:10)
      at Module._compile (internal/modules/cjs/loader.js:618:28)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
      at Module.load (internal/modules/cjs/loader.js:566:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
      at Function.Module._load (internal/modules/cjs/loader.js:498:3)
      at Module.require (internal/modules/cjs/loader.js:598:17)
      at require (internal/modules/cjs/helpers.js:11:18)
  </code></pre>
  <p><strong>update 3</strong>: add <code>package.json</code></p>
  <pre><code>&quot;devDependencies&quot;: {
      &quot;@types/faker&quot;: &quot;^4.1.4&quot;,
      &quot;@types/jest&quot;: &quot;^19.2.4&quot;,
      &quot;@types/node&quot;: &quot;^7.10.0&quot;,
      &quot;babel-cli&quot;: &quot;^6.26.0&quot;,
      &quot;faker&quot;: &quot;^4.1.0&quot;,
      &quot;jest&quot;: &quot;^20.0.4&quot;,
      &quot;ts-jest&quot;: &quot;^20.0.6&quot;,
      &quot;ts-node&quot;: &quot;^3.2.0&quot;,
      &quot;tslint&quot;: &quot;^5.11.0&quot;,
      &quot;typescript&quot;: &quot;^2.9.2&quot;
    },
  </code></pre>