Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

Types do not work #443

Closed
danwellman opened this issue Jun 14, 2017 · 8 comments · Fixed by #581
Closed

Types do not work #443

danwellman opened this issue Jun 14, 2017 · 8 comments · Fixed by #581

Comments

@danwellman
Copy link

danwellman commented Jun 14, 2017

In a project I have installed several @types:

"@types/jasmine": "^2.5.52",
"@types/node": "^7.0.31",

And I have the latest awesome-typescript-loader:

"awesome-typescript-loader": "^3.1.3",

When I build my project, I see messages in the command line output such as:

ERROR in [at-loader] ./test-main.ts:16:18 TS2304: Cannot find name 'require'.

I tried added a types key to my .tsconfig for Node and Jasmine, e.g:

"types": ["node", "jasmine"],

But in this case, the message in the command line output changes to this:

ERROR in [at-loader] TS2688: Cannot find type definition file for 'jasmine'

The .tsconfig file is at the root of the project, at the same level as the node_modules folder.

Changing to the ts-loader fixes the issue without additional config changes (other than using ts-loader in webpack.config.js instead of awesome-typescript-loader)

@s-panferov
Copy link
Owner

s-panferov commented Jun 16, 2017

Do you have moduleResolution setting in your tsconfig.json? If yes, please create a repro case.

@Alino
Copy link

Alino commented Jul 10, 2017

same issue with "awesome-typescript-loader": "^3.2.1",
I got these errors when running webpack

ERROR in [at-loader] TS2688: Cannot find type definition file for 'chrome'.

ERROR in [at-loader] TS2688: Cannot find type definition file for 'jasmine'.

ERROR in [at-loader] TS2688: Cannot find type definition file for 'mocha'.

ERROR in [at-loader] TS2688: Cannot find type definition file for 'node'.

ERROR in [at-loader] TS2688: Cannot find type definition file for 'underscore'.

ERROR in [at-loader] ./app/specs/convertLng.spec.ts:4:1
    TS2304: Cannot find name 'describe'.

all of these types are installed.

tsconfig.json:

{
  "compilerOptions": {
    "removeComments": false,
    "sourceMap": false,
    "noImplicitAny": false,
    "module": "commonjs",
    "moduleResolution": "node",
    "jsx": "react",
    "target": "es5",
    "lib": ["dom", "es6"],
    "declaration": false,
    "noEmitOnError": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "types": [
      "node",
      "mocha",
      "jasmine",
      "underscore",
      "chrome"
    ]
  },
  "include": [
    "src/**/*",
    "specs/**/*"
  ],
  "exclude": [
    "dev/**/*",
    "dist/**/*"
  ]
}

webpack.config.js

module.exports = {
    entry: "./app/index.tsx",
    output: {
        filename: "index.js",
        path: __dirname + "/app/dev/build/"
    },

    devtool: "source-map",

    resolve: {
        extensions: [".ts", ".tsx", ".js", ".json"]
    },

    module: {
        rules: [
            { test: /\.tsx?$/, loader: "awesome-typescript-loader?configFileName=app/tsconfig.json"},
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
        ]
    },

    externals: {
        "react": "React",
        "react-dom": "ReactDOM"
    },
};

@buvinghausen
Copy link

I have the same issue was able to resolve it by simply adding the typeRoots element to compilerOptions as seen below. I think it's because in our cases we were overriding the config file name. I had to override the config file because ts-node picks up my tsconfig.json in order to write my webpack.config in typescript.

"typeRoots": [ "./node_modules/@types" ],

@Nibblesh
Copy link

Nibblesh commented Feb 2, 2018

Hi, I came across this issue today, as one person in our team had the issue but nobody else could recreate it.

After some digging, I have found that it was a Windows only issue, which is possibly why many people have failed to recreate it. I initially started investigating the issue using Ubuntu on windows for bash, as opposed to any of the windows command line options, and I couldn't recreate the issue. After purging all dependency folders and reinstalling via powershell/cmd then the issue started to arise, and by following the fix mentioned by @buvinghausen, everything seems to be working fine.

So by that note I think that this defect is still in the current codebase, and it only effects the windows command line, and it also only effects people with a custom configFileName, if they are depending on the default property for typeRoots (which is by default set to [ "./node_modules/@types" ]).

To recreate the issue, use the configFileName option to point to a file that extends a tsconfig file that is known to work.

USING A WINDOWS MACHINE (with windows command line):

  • Ensure that the tsconfig file that you are extending DOES NOT use the typeRoots property
  • Create an alternative tsconfig file with the following contents
  • Connect your awesome-ts-loader to the new config file
{
  "extends": "./tsconfig.json",
}

run the build and it will fail

@nshew13
Copy link

nshew13 commented Mar 1, 2018

I seem to be in the condition @Nibblesh describes. I'm using Windows 10, my project setup has a custom configFileName, and my typeRoots (whether implicit or explicit) is "./node_modules/@types". Webpack spits dozens of "TS2304: Cannot find name" errors when testing using Jasmine (e.g., TS2304: Cannot find name 'expect'.)

Can this issue be re-opened?

@matthewpelser
Copy link

I have had the same issue where @types are not automatically included in the build when using:

"awesome-typescript-loader": "^5.0.0-1"

Specifically not importing @types/node leaving me with TS2304: Cannot find name 'require'

Using configFileName does not seem to work on the windows command prompt with the following rule:

{ test: /\.ts$/, loaders: [ { loader: 'awesome-typescript-loader', options: { configFileName: helpers.root('.', 'tsconfig.json') } }, 'angular2-template-loader', 'tslint-loader', ] }

This does however work using WSL Bash on Windows 10.

As i don't have a custom tsconfig name and dont need the config option my fix is to just remove it.

matthewpelser added a commit to matthewpelser/angular-seed that referenced this issue Mar 21, 2018
@giladgray
Copy link

@s-panferov this seems to still be a problem: palantir/blueprint#2367. can we re-open the issue to address the bug?

@kevinrstone
Copy link

I encountered this problem today (on Windows 10). I created a custom tsconfig for our karma/jasmine tests to get around performance issues with typescript. I was confused as anything by all the missing global type definitions, such as those for jasmine.

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

Successfully merging a pull request may close this issue.

9 participants