3

I am trying to require shelljs in my core React component.

import React, { Component } from 'react';
import {render} from 'react-dom';
import Header from './components/Header';

const shell = require('shelljs');

class App extends Component {
    render() {
        console.log("First component mounting");
        console.log("First component mounting");
        return (
            <Header />
        )
    }
}

render(<App />, document.getElementById('root'));

When I run

webpack build

I get no errors, then I run my server and I get the following console error.

I'm using jekyll as my server side. Currently transitioning from the normal jekyll implementation to React only. React is well implemented cause i tested the Header component before importing the shelljs module

ReferenceError: process is not defined

I'm new to using modules in javascript, thanks in advance.

6
  • could you add the code/commands you are using to run your server too, please? Commented Jan 22, 2017 at 0:54
  • Updated description Commented Jan 22, 2017 at 1:02
  • yeah, everything i can see looks fine, but i'm not very familiar with jekyll so i might not be much help. sorry. Commented Jan 22, 2017 at 1:06
  • I am not really experience but as reactJs is completely frontend, the backend I use shouldnt even matter. Jekyll is only used to serve the HTML where the Components will be mounted. Correct me if Im wrong Commented Jan 22, 2017 at 1:33
  • yeah, but if you said you built it without any errors first, i guess i'm confused on where that error is coming from Commented Jan 22, 2017 at 1:43

1 Answer 1

8

I'm going to guess you don't really mean to use your shell constant since you haven't referenced it anywhere within your React component. Shelljs looks like it's a tool specifically for the command line.

As for your error:

process is a global variable in the Node environment. Since React runs in the browser, and your component is going to render in the browser, process will not exist in the context of your component.

Try opening the Chrome DevTools (or the developer tools for whatever browser you use) and type in process. You'll get a TypeError because it doesn't exist. What does exist, however, is the global window variable.

Now, open the command line and type node to open the Node.js REPL. Type process here, and you'll see that it's an object holding a lot of properties and values. Next, type window and press enter. window does not exist here because it only exists in the browser.

(Type Ctrl+C twice to exit Node btw. :])

Sign up to request clarification or add additional context in comments.

4 Comments

I want to try to simulate what Jekyll does. I want to have a folder with my posts and see if it's empty, if it isnt, I want to grab all the files as strings and build my stuff from that.
I saw something about what you just said. Thanks for the explanation. Btw any ideas of how can I achieve it?
found out i can do what i wanted with jekyll static files.(jekyllrb.com/docs/static-files) Will try to incorporate a ruby script to count the number of files.
how to fix it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.