Skip to content
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

WebpackError: document is not defined #111

Open
ctrlemo opened this issue Feb 27, 2018 · 2 comments
Open

WebpackError: document is not defined #111

ctrlemo opened this issue Feb 27, 2018 · 2 comments

Comments

@ctrlemo
Copy link

ctrlemo commented Feb 27, 2018

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

@necheporenko
Copy link

The same error...

@JJCLane
Copy link

JJCLane commented Jun 15, 2020

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

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

No branches or pull requests

3 participants