ded / script.js

Asyncronous JavaScript loader and dependency manager
MIT License
2.95k stars 341 forks source link

WebpackError: document is not defined #111

Open ctrlemo opened 6 years ago

ctrlemo commented 6 years ago

I added this package to Gatsby.js React project. when running "gatsby develop" CLI, site works fine. script loads in what is needed. But when trying to create production build, "gatsby build" the build fails because scriptjs tries to execute since it thinks it is in browse environment meanwhile it is not yet. The link below explain the situation better. So if you can update your code to only run when "document" is available would be great. In the meantime I am using method b) by placing the code in componentDidMount(), which is working for me.

See our docs page on debugging HTML builds for help https://goo.gl/yL9lND

10 | else this[name] = definition() 11 | })('$script', function () {

12 | var doc = document | ^ 13 | , head = doc.getElementsByTagName('head')[0] 14 | , s = 'string' 15 | , f = false

WebpackError: document is not defined

necheporenko commented 5 years ago

The same error...

JJCLane commented 4 years ago

I've created a fork with the necessary fix if it helps anyone. https://github.com/JJCLane/script.js/commit/5718da99068600773c3f274f49fa6afcf719369c