ts-loader icon indicating copy to clipboard operation
ts-loader copied to clipboard

Build fails if importing a component from node_modules library

Open ahelmel opened this issue 10 years ago • 48 comments

I added a custom library from node_modules (something like: node_modules/@my-lib) imported a typescript component from this library in my code with: import { MyComponent } from '@my-lib/component/my-component';

The typescript loader fails with the following message: Module build failed: Error: Typescript emitted no output for /Users/.../my-app/node_modules/@my-lib/component/my-component.ts at Object.loader (/Users/.../my-app/node_modules/ts-loader/index.js:456:15).

ts-loader version: 0.8.2 webpack version: 1.13.1 tsc version: 1.8.10

ahelmel avatar Sep 07 '16 09:09 ahelmel

I am having a similar issue, however, it only manifested when I removed a couple entries from my webpack configuration. I have no clue why. The module didn't even output to either of the entries I removed.

I'm using the new typescript/webpack though: ts-loader: 0.8.2 webpack: 2.1.0-beta.21 tsc: 2.0.2

Update: I updated the component in question so that it included precompiled js files. It then worked without an issue. I think the problem is the ts loader may not compile components in node_modules under certain circumstances even if node modules is not excluded in tsconfig.json.

Tiedye avatar Sep 08 '16 00:09 Tiedye

I find a solution : i passed from [email protected] to [email protected]

g3r4n avatar Sep 27 '16 09:09 g3r4n

Enguerrand, Can you please explain how did you fix the error. I am not able to follow your comment related to type script versions.

sumeet70 avatar Oct 03 '16 05:10 sumeet70

Execute the foowing command line :

npm install [email protected] -g
npm install [email protected] -D
npm install [email protected]

It will install typescript v1.8.10 as global package, dev dependencies package and local package.

g3r4n avatar Oct 03 '16 07:10 g3r4n

Also getting this with TS 2.0.3, and downgrading TS is not an option because of some of the things we're using.

Weirdly if I npm link the module in question it works fine, but again that's not really an ideal solution

skysteve avatar Oct 10 '16 15:10 skysteve

If someone is able to produce a repo which demonstrates the problem that could be helpful. From what people have said this repo should work with TS 1.8 but not work with TS 2.0. If that exists then it's a starting point for someone to investigate this.

johnnyreilly avatar Oct 10 '16 15:10 johnnyreilly

@johnnyreilly https://github.com/skysteve/ts-bug-example-2 is about the simplest example I could get. Let me know if you need any more details, but certainly on my mac on node v 6.6 I can replicate the issue.

Update

If I update the tsconfig.json file to remove the "allowJS" flag, the error seems to go away (branch https://github.com/skysteve/ts-bug-example-2/tree/disallow-js). Which is fine in this simple project, but in my real world project I need to mix JS and TS so that's not a proper fix

skysteve avatar Oct 11 '16 11:10 skysteve

Could you retest with ts-loader v0.9.3? We've just added allowJs support which may remedy the issue.

johnnyreilly avatar Oct 16 '16 20:10 johnnyreilly

Problem persists with 0.9.3

stevejhiggs avatar Oct 18 '16 07:10 stevejhiggs

typestyle seems incompatible with ts-loader.

ERROR in ./~/typestyle/csx/index.ts
Module build failed: Error: Typescript emitted no output for /Users/syedb/REPOS/tmp/browsertypestyle/node_modules/typestyle/csx/index.ts
    at Object.loader (/Users/syedb/REPOS/tmp/browsertypestyle/node_modules/ts-loader/dist/index.js:27:15)

Sample project: browsertypestyle.zip

Use npm install and npm start. There we get the error.

Note : awesome-typescript-loader works with it, but it has its own issues that I'd rather not get into here. Mentioning it as its something that could to debugged to figure out root cause / fix :rose:

basarat avatar Nov 09 '16 05:11 basarat

Thanks for the repro @basarat - I'd actually rather forgotten about this issue. I'll try and take a look when I get a moment. Hope you are having fun in the USA - what a time you picked for a holiday!

johnnyreilly avatar Nov 09 '16 06:11 johnnyreilly

Created a PR with a simpler repro : https://github.com/TypeStrong/ts-loader/pull/365 and note about what needs fixing :rose:

basarat avatar Nov 09 '16 06:11 basarat

Advice to any library authors shipping .ts files as the main way for ts consumption: I did the same for typestyle as an experiment and the results were not satisfactory and will never go there again. Reasons covered here : https://github.com/typestyle/typestyle/releases/tag/v0.7.0 Suggest you use outDir + .js + .d.ts as well :rose: ❤️

basarat avatar Nov 13 '16 21:11 basarat

Sounds painful!

johnnyreilly avatar Nov 14 '16 06:11 johnnyreilly

@johnnyreilly the lesson was painful. But the setup isn't:

  • tsconfig
  • code in src/
  • outDir in lib/
  • Done :rose:

basarat avatar Nov 14 '16 06:11 basarat

:tulip: So does the change you were talking about making to ts-loader still stand?

johnnyreilly avatar Nov 14 '16 06:11 johnnyreilly

I have the lost the will to implement it and would not recommend anyone to write a package that ships .ts + .js and instead people should use outDir + .d.ts + .js.

Again for those interested reasons, the following bad things happen if you ship .ts + .js files :

  • If your package is used in nodejs by someone using outDir, your package messes up other peoples outDir option.
  • As new stricter TypeScript compiler options are implemented they cannot be used by people that use your package unless you update your package to compile with those options as well.

Help wanted :rose:

We can provide an additional error message if path contains node_modules that says:

Since the file is in node_modules you should not need to recompile .ts files in node_modules and should contact the package author to request them to use --declaration --outDir. More https://github.com/TypeStrong/ts-loader/issues/278

basarat avatar Nov 14 '16 07:11 basarat

Thanks for the investigation and write up @basarat. What you've said completely makes sense. I like the idea of the error message you suggest. Would it be straightforward to implement?

johnnyreilly avatar Nov 14 '16 07:11 johnnyreilly

Would it be straightforward to implement?

From https://github.com/TypeStrong/ts-loader/blob/a438869c2a6ac97d176af4e5aac268838d4a3350/src/index.ts#L34-L36

    if (outputText === null || outputText === undefined) {
        throw new Error(`Typescript emitted no output for ${filePath}`);
    }

To :

    if (outputText === null || outputText === undefined) {
        const additionalGuidance = filePath.indexOf('node_modules') !== -1 
        ? "You should not need to recompile .ts files in node_modules and should contact the package author to request them to use --declaration --outDir. More https://github.com/TypeStrong/ts-loader/issues/278"
        : "";
        throw new Error(`Typescript emitted no output for ${filePath}.${additionalGuidance}`);
    }

Ofcourse this is untested code and devil will be in the details + adding a test similar to https://github.com/TypeStrong/ts-loader/pull/365 :rose:

basarat avatar Nov 14 '16 08:11 basarat

Nice!

johnnyreilly avatar Nov 14 '16 08:11 johnnyreilly