ded/script.js

WebpackError: document is not defined

ctrlemo opened this issue · 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

The same error...

I've created a fork with the necessary fix if it helps anyone. JJCLane@5718da9