question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript and Webpack import errors

See original GitHub issue

This is related to #649

I’m trying to use Raven in a Typescript project and I can’t get the import to work correctly - I think there is something strange in the typings setup.

In my app I have the following:

import Raven from 'raven-js'
Raven.config('my dsn).install()

This code compiles correctly, but fails at runtime:

Uncaught TypeError: Cannot read property 'config' of undefined(anonymous function) @ index.tsx:12

My first thought was I was including the type definitions for raven-js, but not the code, but if I inspect the compiled JavasScript file I can verify that the raven-js is present.

If I try to import like this:

import * as Raven from 'raven-js'
Raven.config('my dsn').install()

I get a syntax error: error TS2339: Property 'config' does not exist on type 'typeof 'raven-js''., but this does install Raven into the page (it’s not null):

import * as Raven from 'raven-js'
console.log(Raven)

produces:

raven Raven {_hasJSON: true, _hasDocument: true, _lastCapturedException: null, _lastEventId: null, _globalServer: null…}

So when I import Raven one way the type definitions work, but runtime fails, and if I import the other way the type definitions work but runtime fails.

Versions

  • typescript: 2.0.3
  • webpack: 1.13.2
  • npm: 3.9.3
  • node: v6.2.1

tsconfig

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "jsx": "react",
    "noImplicitAny": true,
    "noImplicitThis": true,
    "sourceMap": true,
    "allowJs": true,
    "strictNullChecks": false // TODO - enable this
  },
  "exclude": [
    "custom_typings", "node_modules"
  ]
}

typings

{
  "globalDependencies": {
    ...
    "ravenjs": "registry:dt/ravenjs#0.0.0+20160907111239",
    ...
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
marcomoraincommented, Oct 13, 2016

OK, something really odd is up - if I don’t use typings to install type definitions, but instead include a copy of raven.d.ts directly in my project everything works as expected, using import * as Raven from 'raven-js'.

0reactions
kleebcommented, Jun 13, 2017

Thanks Chris. I am doing it slightly different in the Ionic project:

in copy.config.js I added copy job

  copyRaven: {
    src: ['{{ROOT}}/node_modules/raven-js/dist/raven.min.js', '{{ROOT}}/node_modules/raven-js/dist/raven.min.js.map'],
    dest: '{{BUILD}}'
  }

and simply added raven.min.js to the index.html.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting error with webpack when using import/export
For webpack to be able to resolve typescript file, you need to add it to the list of webpack extensions. By default, webpack...
Read more >
TypeScript - webpack
To make imports do this by default and keep import _ from 'lodash'; syntax in TypeScript, set "allowSyntheticDefaultImports" : true and "esModuleInterop" :...
Read more >
Using webpack with TypeScript - LogRocket Blog
Learn how to use webpack to compile TypeScript to JavaScript and bundle source code into a single JavaScript file.
Read more >
Fixing Errors • AngularJS Migration - codecraft.tv
AngularJS Migration · Step 2 - TypeScript & WebPack; Fixing Errors ... Thankfully, solving this is as simple as adding the following import...
Read more >
Cannot use import statement outside a module [React ...
This error might be raised when using either JavaScript or TypeScript in the back-end. So you could be working on the client side...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found