8

Typescript doesn't find the module in this import import ga from 'googleAnalytics';

SystemJS knows where to find the module, because it has been mapped up front like so:

map: {
    '@angular': 'node_modules/@angular',
    'rxjs': 'node_modules/rxjs',
    'underscore': 'node_modules/underscore/underscore-min.js',
    'googleAnalytics': '//www.google-analytics.com/analytics.js'
},

How can I provide similar mapping to the tsc?

This other question seems to point in a good direction: How to avoid imports with very long relative paths in Angular 2?

Typescript 2.0 seems to support the paths config for the tsconfig.json. Is there a way to download Typescript 2.0? Can I give the path config an http url (//www.google-analytics.com/analytics.js) like I'm doing with SystemJS? If there is no way to download Typescript 2.0, how can I achieve what I want with the current version?

Edit

The specific error I get is: "Cannot find module 'ga'".

Here is my tsconfig.json:

{
    "compilerOptions": {
        "rootDir": "./",
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "noImplicitAny": true
    },
    "exclude": [
        "node_modules",
        "front-end/node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}
4
  • I don't think Typescript 2 has been released yet. github.com/Microsoft/TypeScript/tags Commented May 9, 2016 at 23:59
  • Indeed it doesn't look like it has been released. Commented May 10, 2016 at 4:20
  • what happens if you import it like this import {ga} from 'googleAnalytics'; Commented May 12, 2016 at 7:14
  • Same error. Whether I do import ga, import {ga} or import * doesn't matter, because it doesn't find the module googleAnalytics. The reason I get the error Cannot find module 'ga' is because @jasonszhao told me to rename googleAnalytics to ga (see the only answer below). I admit this is confusing! Commented May 12, 2016 at 19:09

2 Answers 2

1

This problem can be worked around with Typescript Type Definitions.

To use the definition:

  1. Put the type definition in your directory. Premade ones are provided by DefinitelyTyped.
  2. Add this line to the .ts file that needs to import Google Analytics:

    /// <reference path="ga.d.ts" />
    

Update

It did work for me when I ran tsc. Sorry I forgot to add:

  1. Refactor to ga in your imports and SystemJS mapping.

This does not make the compiler know that it needs to check the SystemJS file, but just acts like a placeholder for the module that you import, so that no error is thrown and we can actually resolve the module during runtime with SystemJS.

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't think this would work, but I still tried it (and it didn't work). How is this d.ts file suppose to tell the tsc that to find the googleAnalytics module, it needs to look at the SystemJs mapping (or at least have the same mapping)? Maybe it doesn't need to know the mapping, but only find a corresponding d.ts file, but how would the tsc compiler link a custom module name such as googleAnalytics to ga.d.ts?
It's still not working, maybe because the d.ts doesn't declare a module? github.com/DefinitelyTyped/DefinitelyTyped/blob/master/…
I edited my question with the additional information.
0

i guess you understand that in case of typescript compilation transpiler is looking for definitly type files. He needs to know type definition of external module. I case you have active nodejs module resolution (you have) and you use a non-relative path (you do) you have to add to your project node_modules directory (maybe you already have one) and to this directory add googleAnalytics.d.ts file or you can create node_modules/googleAnalytics directory and then add there index.d.ts. Definition types for google analytics can be downloaded from DefintelyTyped repository

more info about module resolution here

EDIT: according your comment maybe you will have to add modul export to google definition file

declare module 'googleAnalytics' {
    export = UniversalAnalytics;
}

3 Comments

Manually creating a folder in node_modules is a very bad practice. You should rarely (if not never) have modify this folder. The typescript compiler also looks for ambiant module declarations, which is what @jasonszhao proposed, but it seems like the google analytics type definition doesn't export anything.
Excellent i didn't know it.
As a follow up to your edit: you are right, but it would be export = ga instead of export = UniversalAnalytics . Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.