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

Errors thrown on npm linked module even though imports get compiled

Open fis-dan-wnuk opened this issue 11 years ago • 3 comments

I posted a question to StackOverflow, as I'm not entirely sure where the actual issue lies: http://stackoverflow.com/questions/32891778/importing-npm-linked-react-jsx-project-modules-into-angularjs-typescript-module

Relevant dev dependencies:

"style-loader": "~0.12.3",
"css-loader": "~0.18.0",
"url-loader": "~0.5.6",
"file-loader": "~0.8.4",
"html-loader": "^0.3.0",
"http-server": "^0.8.0",
"imports-loader": "~0.6.4",
"babel-core": "~5.8.25",
"babel-loader": "~5.3.2",
"typescript": "~1.6.2",
"sass-loader": "^2.0.1",
"ts-loader": "~0.5.5",
"ng-cache-loader": "~0.0.13",
"power-assert": "~1.0.1",
"require-dir": "~0.3.0",
"webpack": "~1.12.1",
"webpack-dev-server": "^1.11.0",
"extract-text-webpack-plugin": "~0.8.2"

Relevant webpack config:

var path = require("path");
var webpack = require("webpack");
var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: './src/components/index.ts',
  output: {
    path: './dist',
    publicPath: 'http://localhost:8090/assets',
    filename: 'sample.framework.js'
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract("style!css")
      },
      {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract("css!sass")
      },
      {
            test: /\.html$/,
            loader: "ng-cache?prefix=components/[dir]",
            exclude: [/node_modules/]
  },
      {
            test: /\.tsx?$/,
            loader: "ts?stage=0",
            exclude: [/node_modules/]
      },
      {
            test: /\.jsx?$/,
            loader: "babel?stage=0"
      }
    ]
  },
  resolve: {
    extensions: ['', '.ts', '.tsx', '.js','.jsx', '.scss'],
    modulesDirectories: ['node_modules']
  },
  externals: {
    'angular': 'angular',
    'jQuery': '$',
    'react': 'React'
  },
  cache: true,
  plugins: [
    new ExtractTextPlugin("sample.framework.css")
  ]
};

Here's the full error trace:

webpack result is served from http://localhost:8090/assets
content is served from C:\workspaces\UI\sample-react-framework-wrapper
 10% 0/1 build modulests-loader: Using [email protected]
Hash: 8e984ad48a73c8891cc8
Version: webpack 1.12.2
Time: 4963ms
                               Asset     Size  Chunks             Chunk Names
0301a776afbfe3cf35bf65b24a255d93.jpg   571 kB          [emitted]
                 sample.framework.js  61.8 kB       0  [emitted]  main
chunk    {0} sample.framework.js (main) 58.6 kB [rendered]
    [0] ./src/components/index.ts 112 bytes {0} [built]
    [1] ./src/components/banner/banner.ts 2.44 kB {0} [built] [1 error]
    [3] ../sample-react-framework/src/index.js 862 bytes {0} [built]
    [4] ../sample-react-framework/src/components/banner/banner.jsx 8.67 kB {0} [built]
    [5] ../sample-react-framework/~/classnames/index.js 1.12 kB {0} [built]
    [6] ../sample-react-framework/src/components/banner/banner.scss 3.08 kB {0} [built] [failed]
    [7] ../sample-react-framework/~/css-loader/lib/css-base.js 1.48 kB {0} [built]
    [8] ../sample-react-framework/src/images/FIS_logo_default.jpg 81 bytes {0} [built]
    [9] ../sample-react-framework/src/components/side-menu/side-menu.jsx 11.8 kB {0} [built]
   [10] ../sample-react-framework/src/components/side-menu/side-menu.scss 3.33 kB {0} [built] [failed]
   [11] ../sample-react-framework/src/components/navigation/navigation.jsx 9.84 kB {0} [built]
   [12] ../sample-react-framework/src/components/navigation/navigation.scss 9.15 kB {0} [built] [failed]
   [13] ../sample-react-framework/src/images/Home_white.png 1.75 kB {0} [built]
   [14] ./src/components/navigation/navigation.ts 2.37 kB {0} [built] [1 error]
   [16] ./src/components/side-menu/side-menu.ts 2.44 kB {0} [built] [1 error]
     + 2 hidden modules

ERROR in ./src/components/side-menu/side-menu.ts
(7,26): error TS2307: Cannot find module 'sample-react-framework'.

ERROR in ./src/components/navigation/navigation.ts
(7,28): error TS2307: Cannot find module 'sample-react-framework'.

ERROR in ./src/components/banner/banner.ts
(6,24): error TS2307: Cannot find module 'sample-react-framework'.

ERROR in ../sample-react-framework/src/components/banner/banner.scss
Module build failed: (7,26): error TS2307: Cannot find module 'sample-react-framework'.

ERROR in ../sample-react-framework/src/components/side-menu/side-menu.scss
Module build failed: (7,26): error TS2307: Cannot find module 'sample-react-framework'.

ERROR in ../sample-react-framework/src/components/navigation/navigation.scss
Module build failed: (7,26): error TS2307: Cannot find module 'sample-react-framework'.
Child extract-text-webpack-plugin:
    chunk    {0} extract-text-webpack-plugin-output-filename 4.64 kB [rendered]
        [0] ../sample-react-framework/~/css-loader!../sample-react-framework/~/sass-loader!../sample-react-framework/src/components/banner/banner.scss 3.08
 kB {0} [built]
        [1] ../sample-react-framework/~/css-loader/lib/css-base.js 1.48 kB {0} [built]
        [2] ../sample-react-framework/src/images/FIS_logo_default.jpg 81 bytes {0} [built]

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\side-menu\side-menu.ts
    (7,26): error TS2307: Cannot find module 'sample-react-framework'.

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\navigation\navigation.ts
    (7,28): error TS2307: Cannot find module 'sample-react-framework'.

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\banner\banner.ts
    (6,24): error TS2307: Cannot find module 'sample-react-framework'.
Child extract-text-webpack-plugin:
    chunk    {0} extract-text-webpack-plugin-output-filename 4.81 kB [rendered]
        [0] ../sample-react-framework/~/css-loader!../sample-react-framework/~/sass-loader!../sample-react-framework/src/components/side-menu/side-menu.scs
s 3.33 kB {0} [built]
        [1] ../sample-react-framework/~/css-loader/lib/css-base.js 1.48 kB {0} [built]

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\side-menu\side-menu.ts
    (7,26): error TS2307: Cannot find module 'sample-react-framework'.

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\navigation\navigation.ts
    (7,28): error TS2307: Cannot find module 'sample-react-framework'.

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\banner\banner.ts
    (6,24): error TS2307: Cannot find module 'sample-react-framework'.
Child extract-text-webpack-plugin:
    chunk    {0} extract-text-webpack-plugin-output-filename 12.4 kB [rendered]
        [0] ../sample-react-framework/~/css-loader!../sample-react-framework/~/sass-loader!../sample-react-framework/src/components/navigation/navigation.s
css 9.15 kB {0} [built]
        [1] ../sample-react-framework/~/css-loader/lib/css-base.js 1.48 kB {0} [built]
        [2] ../sample-react-framework/src/images/Home_white.png 1.75 kB {0} [built]

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\side-menu\side-menu.ts
    (7,26): error TS2307: Cannot find module 'sample-react-framework'.

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\navigation\navigation.ts
    (7,28): error TS2307: Cannot find module 'sample-react-framework'.

    ERROR in C:\workspaces\UI\sample-react-framework-wrapper\src\components\banner\banner.ts
    (6,24): error TS2307: Cannot find module 'sample-react-framework'.

webpack: bundle is now VALID.

If I run "npm run build" the JS bundle gets built (in /dist/sample.framework.js) - but the /dist/sample.framework.css does not get built - likely because it is running into this odd error in the SCSS files as well, which are the only CSS files in the projects. The JS file also only contains the imported modules (from the npm linked project) and not the actual TypeScript module code in the wrapper project.

Since it is able to track through to the linked project and compile its files I am not entirely sure why this error comes up after the fact. Or also why it is impacting the .scss files as well. Would really appreciate any help - and I apologize if this isn't the correct place.

fis-dan-wnuk avatar Oct 01 '15 18:10 fis-dan-wnuk

From your SO question:

import { Banner } from 'sample-react-framework';

The error here is that TypeScript is saying it doesn't understand sample-react-framework. TypeScript doesn't understand plain JS modules. It only understands TS modules or declaration files that describe the API of the JS module. Assuming you want to keep sample-react-framework as pure JS and not TypeScript, you'll need to create a definition file for it. For example...

// index.d.ts
import React from 'react';
export class Banner extends React.Component { }

Then in the package.json of sample-react-framework you could put:

"typings": "index.d.ts"

TypeScript should then be able to resolve the definition file and understand what you mean when you say import { Banner } from 'sample-react-framework';.

jbrantly avatar Oct 02 '15 00:10 jbrantly

Well I feel silly!

I appreciate the quick reply. After trying this out you are correct and everything is now working properly.

Thanks so much!

fis-dan-wnuk avatar Oct 02 '15 13:10 fis-dan-wnuk

This. I was on the verge of insanity until I found this. Thanks for clarifying.

randseay avatar May 05 '16 01:05 randseay