Skip to content

TypeError due to assuming all <script> elements have a src attribute #117

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
steveme opened this issue Feb 25, 2015 · 26 comments
Closed

TypeError due to assuming all <script> elements have a src attribute #117

steveme opened this issue Feb 25, 2015 · 26 comments

Comments

@steveme
Copy link

steveme commented Feb 25, 2015

Hi,

Am seeing a lot of :

Uncaught TypeError: Cannot read property 'replace' of null webpack-dev-server.js:1

Which is from here:
https://github.com/webpack/webpack-dev-server/blob/master/client/index.js#L5

As i've got script elements in my markup that don't have a src attribute.

We're using fluxible, so our HTML has inline state in a <script/> tag like: https://github.com/yahoo/flux-examples/blob/master/fluxible-router/components/Html.jsx

Thanks.

Steve

@jhnns
Copy link
Member

jhnns commented Feb 25, 2015

Yep, that's an error... that's probably not a good fallback to get the base path. @sokra when is __resourceQuery not set?

@sokra
Copy link
Member

sokra commented Feb 25, 2015

It only checks the current script tag, which should be the script tag for the webpack-dev-server client. __resourcePath is used when putting the webpack-dev-server/client?... in the entry. I don't think this is wrong. I think the error is somewhere else:

  • <script> tag to the WDS client is included with async, defer or inlined
  • webpack-dev-server/client is added to the entry point but no host argument is included.

@voronianski
Copy link

@sokra @steveme @jhnns I have very similar issue:

webpack-dev-server --hot --inline --progress --colors

70% 1/1 build modules
TypeError: Object /Users/me/github/app-webpack/node_modules/webpack-dev-server/client/index.js?http://localhost:8080,webpack/hot/dev-server,webpack/hot/dev-server has no method 'replace'
at Tapable.<anonymous> (/Users/me/github/app-webpack/node_modules/webpack/lib/NormalModuleFactory.js:42:26)

webpack config:

'use strict';

var webpack = require('webpack');

module.exports = {
    entry: [
        'webpack/hot/dev-server',
        './src/app.js'
    ],
    output: {
        path: __dirname + '/public/build/',
        filename: 'bundle.js',
        publicPath: '/build/'
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    module: {
        loaders: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loaders: ['babel-loader?experimental']
            }
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
    ]
};

base.html is served via express server where {{mainJS}} is passed to locals as 'http://localhost:8080/build/bundle.js' (also want to figure out how better serve this in production):

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>App Webpack</title>
</head>
<body>
    <div id="app"></div>
    <script src="{{mainJS}}"></script>
</body>
</html>

@Zache
Copy link

Zache commented Jan 19, 2016

This happens to me because Google Tag Manager inserts a script element at the bottom of the page and Webpack tries to replace the last script element, which no longer is the bundle...

@iest
Copy link

iest commented Feb 2, 2016

Get this exact error too, what @Zache said. Fixed by removing GTM in dev...

@MattKunze
Copy link

I'm getting this when I have the redux-devtools Chrome extension active - the extension injects a script into the document which doesn't have a src attribute

@micha149
Copy link

I can confirm this problem with the redux devtools chrome extension

@tiye
Copy link
Contributor

tiye commented Feb 15, 2016

Because of defer in our case. I'd like to drop defer.

@3LOK
Copy link

3LOK commented Feb 20, 2016

We get it because we lazy load our scripts...

@hijonathan
Copy link

👀 me too

@ptz0n
Copy link

ptz0n commented Apr 9, 2016

Same here. Seems to be related to the defer attribute of the script element.

Update: #447 solves the issue. 👍

@brotzky
Copy link

brotzky commented May 10, 2016

@Zache the GTM issue was never resolved, correct? I still get Uncaught TypeError: Cannot read property 'replace' of null with GTM in my index.html. This occurs on both 1.14.0 and 1.14.1 for me.

@Zache
Copy link

Zache commented May 11, 2016

@brotzky I'm not sure, it wasn't GTM itself but rather the tags and our workaround was to remove them from our development env.

@brotzky
Copy link

brotzky commented May 11, 2016

@Zache, I think we will take the same approach. Thanks!

@ptz0n
Copy link

ptz0n commented May 15, 2016

Sorry, but I don't get what's hindering us from merging either #447 or #394.

Please merge or clarify 😸

@jhnns
Copy link
Member

jhnns commented May 15, 2016

I've just merged #447 – but now I think, this PR is only removing the error instead of being an actual solution to the problem.

When the url can't be retrieved from the script tag, it will default to / which might be correct in some cases, in some others, however, it will not be correct.

Wouldn't it be better to use document.currentScript? Unfortunately, there are no tests so I don't know if that change would break anything :(

@achimnol
Copy link

I have faced this issue with deferred ZenDesk script injection. Looking forward to find a good solution!

@brotzky
Copy link

brotzky commented Jul 18, 2016

@achimnol
@Zache's solution is the simplest. We have a dev and prod index.html that has different 3rd party scripts in them (GTM, LiveChat, Polyfills/Shims, etc). Use the dev index.html in development and then point webpack at the production index.html when running a build. Proven to be the simplest solution for now.

@SpaceK33z
Copy link
Member

@jhnns, document.currentScript looks exactly like the thing we want. It also supports scripts that run with async, so it will solve many other issues that were reported.

The only problem is that document.currentScript does not work for IE 6-11. There is a polyfill available, which supports IE6-10, but not IE11.

So what should we do here? Do we support IE?

@reem
Copy link

reem commented Aug 31, 2016

I and several other members of my team are hitting this issue as well and I believe it is just because of chrome extensions that we have. It would be really great if the fix from #447 was backported to v1 even if it's only a temporary solution. Right now we are just stuck refreshing the page until it works, which is a very frustrating side effect of an otherwise amazing tool!

@SpaceK33z
Copy link
Member

SpaceK33z commented Sep 1, 2016

This issue was fixed in PR #496. It now behaves like this:

  1. Use document.currentScript if available. This is the most reliable way to get the current script, and works in most browsers.
  2. Fallback to document.scripts.
  3. If that fails, throw an exception.

I'm not sure about backporting this to the 1.x branch, because it can potentially break existing setups.

@jhnns
Copy link
Member

jhnns commented Sep 1, 2016

Awesome! 👍 Great work!

@SpaceK33z
Copy link
Member

Released in 2.1.0-beta.3.

@Braincompiler
Copy link

Is it also fixed in 1.16.x? We cannot use webpack2 (it tries to resolve our dynamic module loading approach using System.import and we cannot prevent webpack2 from doing this).

@SpaceK33z
Copy link
Member

@Braincompiler, yes, this was backported in 1.15.2.

@aryzing
Copy link

aryzing commented Sep 25, 2018

Not sure if this issue is back, but I'm seeing the error

Warning: React DevTools encountered an error: TypeError: Cannot read property 'replace' of null

as reported by #381. This error is reported the very first time webpack HMRs. Subsequent HMRs do not cause this error to show up. Let me know if you need a repo reproducing the issue.

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

No branches or pull requests