Skip to content

cannot import modules #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
itajaja opened this issue May 23, 2015 · 13 comments
Closed

cannot import modules #207

itajaja opened this issue May 23, 2015 · 13 comments
Labels

Comments

@itajaja
Copy link

itajaja commented May 23, 2015

If i use the -m commonjs option from cli then I can do this in the code:

import {Component, View, bootstrap} from 'angular2/angular2';

but in ST the plugin marks the line as an error saying:

Cannot compile external modules unless the '--module' flag is provided

how do I provide command line options to the plugin?

@billti
Copy link
Member

billti commented May 23, 2015

To specify options for the editor that you would normally specify on the command-line for the compiler, add a tsconfig.json file to the directory (or a parent directory) of your code. The Sublime plugin for TypeScript will use the settings in this file. See https://github.com/Microsoft/TypeScript/wiki/tsconfig.json

@itajaja
Copy link
Author

itajaja commented May 25, 2015

Oh. Thanks :) Do you think it's a welcomed PR if I make this more visible in the readme of the ST plugin?

@robertdp
Copy link

robertdp commented Jun 4, 2015

add a tsconfig.json file to the directory (or a parent directory) of your code.

@billti, I'm still getting that error even with the file, but only when editing files that are 2 or more folders deep. It seems as though the plugin is only looking in the file's current folder and the one above that.

There are no errors when compiling the project, but Sublime has red squiggles in almost every file.

@breezewish
Copy link

my typescript compiler even does not lookup tsconfig.json in the file's current folder :-( However typing tsc in the terminal is okay. Any ideas?

Build output:

/Users/Breezewish/Desktop/test/vj4.ts(1,1): error TS1148: Cannot compile modules unless the '--module' flag is provided.
/Users/Breezewish/Desktop/test/vj4.ts(1,20): error TS2307: Cannot find module 'jquery'.
[Finished in 1.0s with exit code 2]
[cmd: ['/usr/local/bin/node', '/Users/Breezewish/Library/Application Support/Sublime Text 3/Packages/TypeScript/tsserver/tsc.js', '/Users/Breezewish/Desktop/test/vj4.ts']]
[dir: /Users/Breezewish/Desktop/test]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true
  },
  "files": [
    "typings/tsd.d.ts"
  ]
}

From terminal:

Breezewish@SummerWishMac: ~/Desktop/test
 $ pwd
/Users/Breezewish/Desktop/test

Breezewish@SummerWishMac: ~/Desktop/test
 $ tsc

Breezewish@SummerWishMac: ~/Desktop/test
 $ 

@ulritech
Copy link

ulritech commented Jan 8, 2016

I also had problems with this plugin working for files that were deeply nested in the directory structure. I use webpack and had originally set the tsconfig files to 2 files: my entry point and my typings file. With this configuration, sublime showed red squiggles everywhere and the typescript auto completion feature did not work.

Somewhere, I recalled a user reporting that this plugin worked as expected if every ts file in his project was listed in the tsconfig files array (and he suggested using gulp to keep this in sync). Sure enough, listing each ts file in the tsconfig files array works - the red squiggles no longer appear and auto completion also works.

The ArcticTypescript package describes a filesGlob property that can use wildcards. I replaced the files property with filesGlob as they describe and boom - everything started working for all my files without the need for a gulp workflow to update this automatically.

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "sourceMap": true,
        "removeComments": false,
        "target": "es5"
    },
    "filesGlob": [
        "./**/*.ts",
        "!./node_modules/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "dist"
    ]
}

@bartzy
Copy link

bartzy commented Jan 11, 2016

@ulritech
This works for me - thanks!

@zhengbli
Copy link
Contributor

@summerwish This is because your tsconfig.json didn't include your vj4.ts file in your project. Therefore when you try to build the vj4.ts in the plugin, the compiler complains because the file doesn't belong to a configured project. However, when you run tsc in the command line, your are only compiling the typings/tsd.d.ts, and it is understandable why it was done successfully.

To solve the issue, as @ulritech suggested above try to add all source files of your project in your files array, or do not specify a files array to implicitly include every files in the directory. More information can be found at the wiki page

@bartzy
Copy link

bartzy commented Jan 12, 2016

@zhengbli I used the filesGlob config @ulritech provided above, and it works. I didn't find documentation for filesGlob - Is it supported publicly?

Thanks.

@zhengbli
Copy link
Contributor

There is an open PR for the filesGlob support (microsoft/TypeScript#5980), though it is not merged in yet. The one came with Arctic​Typescript is their own implementation I believe.

@bartzy
Copy link

bartzy commented Jan 12, 2016

That's weird - How my config works then?
My TS version is 1.7.5. I think the plugin uses 1.7.3 though.

{
    "compilerOptions": {
        "module": "es6",
        "target": "es6",
        "sourceMap": true,
        "noImplicitAny": true,
        "removeComments": false
    },
    "filesGlob": [
        "./**/*.ts",
        "!./node_modules/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "dist"
    ]
}

@zhengbli
Copy link
Contributor

It probably is a coincident. Assuming you are not using the ArcticTypeScript plugin, the filesGlob property will be discarded by the language service because it is not supported. Then it is the equivalent to a tsconfig file without files array, therefore all source files except folder / files in the exclude list will be included. And if you have //// <reference/> in your code for the ts files within node_modules folder, those will be included too.

@bartzy
Copy link

bartzy commented Jan 12, 2016

Got it.

So for now, until filesGlob is supported, I should include all files I want to compile in the files array?

@zhengbli
Copy link
Contributor

You can, though you definitely can do without the files array and implicitly include every ts files in the folder, and put stuff you don't want in the exclude array, if that works for you.

Zhengbo

From: Bar Ziony [email protected]
Sent: Jan 12, 2016 1:00 AM
To: Microsoft/TypeScript-Sublime-Plugin
Cc: Zhengbo Li
Subject: Re: [TypeScript-Sublime-Plugin] cannot import modules (#207)

Got it.

So for now, until filesGlob is supported, I should include all files I want to compile in the files array?

Reply to this email directly or view it on GitHubhttps://github.com//issues/207#issuecomment-170844084.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants