thumb

Recently, when attempting to use a gulp command in the Terminal app, I saw an error message saying ReferenceError: path is not defined. In this tutorial, you will learn how to fix the error that leads to this error message. How to fix: ReferenceError: path is not defined

What causes this error

In my case, the below error appeared when attempting to run the gulp command inside my app project directory in the Terminal app.

$ gulp start
/Users/arthur/app/gulpfile.js:23
        path = {
             ^

ReferenceError: path is not defined
    at /Users/arthur/app/gulpfile.js:23:14
    at Object.\<anonymous\> (/Users/arthur/app/gulpfile.js:111:3)
    at Module.\_compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module.\_extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module.\_load (internal/modules/cjs/loader.js:896:14)
    at Module.require (internal/modules/cjs/loader.js:1036:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at execute (/usr/local/lib/node_modules/gulp-cli/lib/versioned/^4.0.0/index.js:36:18)
    at Liftoff.handleArguments (/usr/local/lib/node_modules/gulp-cli/index.js:201:24)

The error message above is self explanatory. It saying that the gulp cannot access the path variable (or constant), and this is because path is not defined. From the /Users/arthur/app/gulpfile.js:23 line of the error message, we know that this happened in the gulpfile.js file on line 23, and the problem line looks like path = {.

In my case, this makes sense because I know that I trying new things in the gulpfile.js file, and before the changes I made in this file, everything was fine; gulp was running without errors. Therefore, something is wrong with the changes I made.

Here is a snippet from my gulpfile.js file after the error appeared:


const

    // Modules
    { src, dest, series, parallel } = require('gulp'),
    { spawn } = require('child_process'),
    fs        = require('fs'),
    del       = require('del'),
    gulpif    = require('gulp-if');

    // Directory locations
    path = {
        source  : './source',
        build   : './docs'
    };

In the snippet above, I define constants through a comma-separated list, specifying the reserved word const only once, at the beginning of that list of constants. You can see that the path constant is not missing, it’s there. But the previous constant ends with a semicolon (;) instead of a comma (,).

Now we know what caused this error.

How to solve it

Once we know what is causing the gulp run to fail, we can use it to solve the problem. It is a really easy process. To solve this problem, we need to replace a semicolon (;) with a comma (,) in the gulpfile.js file. Now, step by step guide.


Open the file gulpfile.js in the code editor you prefer. I use the Atom (atom.io) app developed by GitHub.


In the file gulpfile.js, scroll down to the line indicated in your error message.

Note! You can find the exact line number in your error message. Look at the path to the gulpfile.js file. In my case, the /Users/arthur/app/gulpfile.js:23 line of the error message, tells me that I need the line 23 of the gulpfile.js file. For more information, scroll up to the “What causes this error” section.


Replace the semicolon (;) with the comma (,) at the end of the constant that precedes the constant indicated in your error message.

Example. In my gulpfile.js file, I need to replace the line gulpif = require('gulp-if'); with gulpif = require('gulp-if'),. This is how my gulpfile.js file looks after editing:

Note! The contents of your gulpfile.js file may look different.


const

    // Modules
    { src, dest, series, parallel } = require('gulp'),
    { spawn } = require('child_process'),
    fs        = require('fs'),
    del       = require('del'),
    gulpif    = require('gulp-if'),

    // Directory locations
    path = {
        source  : './source',
        build   : './docs'
    };


Save the changes made to the file gulpfile.js.

Conclusion

That’s it, you’re done. Now the gulp should run without the ReferenceError: path is not defined error. So simple isn’t it?

If you are having trouble fixing this problem with the instructions above, but are being able to solve this problem with any another method please describe it in the comment section below. Thanks!

I hope this article has helped you learn how to fix the ReferenceError: path is not defined error. If this article helped you then please leave a comment :smiley:

Thanks for reading!