Last updated: Feb 29, 2024
Reading timeยท4 min
If you got the error in a Node.js project, click on the second subheading.
The "TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type
string. Received type undefined" occurs when you have an outdated or glitched
version of react-scripts
.
To solve the error, upgrade your version of react-scripts
and delete and
reinstall your node modules if necessary.
The first thing you should try is to upgrade your version of the react-scripts
package.
# ๐๏ธ with NPM npm install react-scripts@latest # -------------------- # ๐๏ธ with YARN yarn add react-scripts@latest
If you get an error when running the command, use the --force
flag.
npm install react-scripts@latest --force
The issue is sometimes caused by running the npm audit fix command.
If the error persists, issue the npm info react-scripts version
command.
npm info react-scripts version
Open your package.json
file and manually update the version of the
react-scripts
package.
For example, the output of the command shows 5.0.1
in my case, so I'd set the
version of react-scripts
in package.json
to 5.0.1
.
{ "dependencies": { "react-scripts": "5.0.1" } }
After you update the version of the package, rerun the npm install
command.
# ๐๏ธ with NPM npm install # -------------------- # ๐๏ธ with yarn yarn
If the error is not resolved, try to delete your node_modules
and
package-lock.json (not
package.json
) files, re-run npm install
and restart your dev server.
# ๐๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # ๐๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ with NPM npm install # ๐๏ธ or with YARN yarn
webpackDevServer.config.js
if you ejected your React AppIf you ejected your app by running the npm run eject
command, you have to
update the config/webpackDevServer.config.js
file in your project to resolve
the error.
Open your config/webpackDevServer.config.js
and update the following line:
app.use(noopServiceWorkerMiddleware());
To the following line:
app.use(noopServiceWorkerMiddleware('/'));
Try to restart your development server after making the change.
If the error persists, try to upgrade the versions of all react-related packages in your project.
# ๐๏ธ with NPM npm install react-scripts@latest react@latest react-dom@latest # -------------------- # ๐๏ธ with YARN yarn add react-scripts@latest react@latest react-dom@latest
The react-scripts
package only supports specific versions of react
and
react-dom
, so you might have a mismatch.
If you get an error, try running the command with the --force
flag.
npm install --force react-scripts@latest react@latest react-dom@latest
The Node.js "TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of
type string. Received type undefined" occurs when you pass an undefined
value
to a path
method.
To solve the error, make sure to pass a string to the method.
Here is an example of how the error occurs.
import path from 'path'; // โ๏ธ TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined console.log(path.resolve(undefined));
We passed an undefined
value to the path.resolve()
method which caused the
error.
You can console.log()
the value you are passing to the method to verify it is
undefined
.
undefined
property on an objectYou are likely accessing an undefined
property on an object.
When you access an object property that doesn't exist, an undefined
value is
returned.
import path from 'path'; const obj = {}; console.log(obj.path); // undefined // โ๏ธ TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined console.log(path.resolve(obj.path));
The path
property doesn't exist on the object, so trying to access it returns
undefined
.
One way to solve the error is to use the logical OR (||) operator to provide a
fallback value of an empty string if the value is undefined
.
import path from 'path'; const obj = {}; console.log(obj.path); // undefined // ๐๏ธ /home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js console.log(path.resolve(obj.path || ''));
If the value to the left is equal to undefined
an empty string is returned, so
the error is no longer raised.
You can also use the nullish coalescing (??) operator to provide a fallback.
import path from 'path'; const obj = {}; console.log(obj.path); // undefined // ๐๏ธ /home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js console.log(path.resolve(obj.path ?? ''));
If the value to the left of the
nullish coalescing operator (??)
is equal to null
or undefined
, the value to the right is returned,
otherwise, the value to the left of the operator is returned.
If the value to the left is undefined
, an empty string is returned.
You can use an if
statement if you need to check if the value is undefined
before passing it to a method.
import path from 'path'; const obj = {}; if (obj.path) { const result = path.resolve(obj.path); console.log(result); } else { // ๐๏ธ this runs console.log('The value is undefined'); }
The code sample uses an if
statement to check if the variable stores a truthy
value before passing it to the path.resolve()
method.
We only call the method if the variable doesn't store an undefined
value,
otherwise, the else
block runs.
If you meant to use the __dirname
variable to get the absolute path of the
current directory, use the following code sample.
import path from 'path'; import {fileURLToPath} from 'url'; const __filename = fileURLToPath(import.meta.url); console.log('file name: ', __filename); const __dirname = path.dirname(__filename); console.log('directory name: ', __dirname); // ๐๏ธ /home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/another console.log(path.resolve(__dirname, 'another'));
The code sample uses the fileURLToPath
and the path.dirname()
methods to
construct the absolute path to the directory.