script.js icon indicating copy to clipboard operation
script.js copied to clipboard

WebpackError: document is not defined

Open ctrlemo opened this issue 6 years ago • 2 comments

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

  • script.js:12 ~/scriptjs/dist/script.js:12:1

  • script.js:8 doc ~/scriptjs/dist/script.js:8:1

  • script.js:11 Object.module.exports.layoutContext ~/scriptjs/dist/script.js:11:2

  • index.js:5 Object.module.exports.siteMetadata.title src/layouts/index.js:5:1

  • index.js:3 Object.exports.__esModule .cache/layouts/index.js:3:3

  • sync-requires.js:6 Object.exports.__esModule .cache/sync-requires.js:6:35

ctrlemo avatar Feb 27 '18 06:02 ctrlemo

The same error...

necheporenko avatar Jul 12 '19 14:07 necheporenko

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

JJCLane avatar Jun 15 '20 16:06 JJCLane