Search code examples
typescriptnpmqtsc

error TS1316 Global module exports may only appear at top level


Having trouble building an old typescript project. Unable to build an old project that I am coming back to. There is an issue with q when trying to build. I assume it is my tsc version, but every version I have tried results in errors.

There were some suggestions online to change the typescript version to past 2.2 because it handles typings differently, but nothing has resulted in any progress.

I have provided error information, and versions. Also my tsconfig.json. Please let me know if you need more information.

Errors ($tsc -v 2.1.5)

$ tsc -p ./
typings/modules/q/index.d.ts(10,1): error TS1316: Global module exports may only appear at top level.

Versions

nvm: 1.1.5
npm: 4.1.2
tsc: 2.1.5
node: 7.5.0

$ npm list --depth=0
+-- @types/[email protected]
`-- [email protected]

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "amd",
        "outDir": "static/js",
        "sourceMap": true,
        "strictNullChecks": true,
        "noImplicitAny": true
    },
    "files": [
        "typings/index.d.ts",
        "../common/types.d.ts"
    ],
    "include": [
        "src/**/*.ts"
    ]
}

EDIT

The errors listed under tsc v2.3.0 were actually still 2.1.5. I didn't install typepscript globally, so it was still using 2.1.5 instead of 2.3.0 when I ran the tsc command.


Solution

  • When errors show up in declaration files, it's generally an indication that the declaration file uses syntax which is not available in your version of TypeScript.

    In this case, let's take a look at the lines reported as errors.

    64: then<U = T, V = never>(onFulfill?: ...
    200: thenReject<U = T>(reason?: any): Promise<U>;
    

    The errors reported here point to the equal signs in the type arguments section. This indicates that default type arguments aren't supported. This doesn't really make sense since support for default type arguments was added in TS2.3. My best guess is that you didn't actually have 2.3 installed for those errors, and the versions were switched...

    With that in mind, the other errors are likely due to incompatibility between the old (<2.1, if I recall correctly) typings files and the new @types files. With the types installed in node_modules/@types/q, you don't need to include a custom typings folder, so you should be able to delete typings/modules/q (in fact, the whole typings folder) to resolve the other error.

    If this doesn't resolve your issue, a project to clone would make it much easier to figure out what's wrong.