Search code examples
angulartypescriptecmascript-6protractorcommonjs

Simple import of module from one .ts file to another


I am having problem importing classes from one .ts file to another .ts file.

This is my project structure:

enter image description here

I am trying to import print.ts to testing.ts.

This is what my tsconfig.json looks like:

enter image description here

Contents of my testing.ts:

import { names } from 'testing';
console.log(names.printFirstName());

Contents of my print.ts:

class nameCollection {
static printFirstName(){
    return 'OYEAAAH';
    };
}

export {nameCollection as names};

I am running my tests using: node testing.js

And I am getting: enter image description here

Thanks!


Solution

  • Typescript does path mapping for module resolution, you are mixing directories and files

    {
      "compilerOptions": {
        "paths": {
          "testing/*": ["src/nested/*"]
        }
      }
    }
    

    and then in your scripts you can import import {names} from 'testing/print';

    check https://www.typescriptlang.org/docs/handbook/module-resolution.html for more details