Fixing ERR_INVALID_ARG_TYPE (path) in Node.js
The TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined error in the terminal appears when you pass undefined instead of a string to a path function such as path.join. The most common cause is running a script without an environment variable such as process.env.DATA_DIR. If you pass the value correctly, the error goes away.
The same error also happens when you wrote the setting in a .env file but it is not loaded, or when the environment variable name is misspelled. In my test environment I triggered the error, recorded the full message, and confirmed that the error goes away with each of the fixes below.
The test environment was Ubuntu 24.04 with Node.js 22.23.4 (npm 10.9.9). I did not test Windows or macOS, other Node.js versions, or ways of reading .env files with packages such as dotenv.
The full error
In my test environment (Ubuntu 24.04, Node.js 22.23.4), I wrote only "hello" to data/message.txt and created the following index.js. The code reads message.txt from the folder given by the environment variable DATA_DIR and prints its contents.
const path = require('node:path');
const fs = require('node:fs');
const dataDir = process.env.DATA_DIR;
const file = path.join(dataDir, 'message.txt');
console.log(fs.readFileSync(file, 'utf8').trim());
When I ran node index.js without setting DATA_DIR, it stopped with the following output (exit code 1; the paths are replaced with example values).
$ node index.js
node:internal/errors:540
throw error;
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at Object.join (node:path:1354:7)
at Object.<anonymous> (/path/to/app/index.js:5:19)
at Module._compile (node:internal/modules/cjs/loader:1781:14)
at Object..js (node:internal/modules/cjs/loader:1913:10)
at Module.load (node:internal/modules/cjs/loader:1505:32)
at Function._load (node:internal/modules/cjs/loader:1309:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:254:19)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
at node:internal/main/run_main_module:36:49 {
code: 'ERR_INVALID_ARG_TYPE'
}
Node.js v22.23.4
The main part of the error is the TypeError line. It means that the path argument must be a string, but it received undefined. ERR_INVALID_ARG_TYPE is listed in the official error code list as "a Node.js API was passed an argument of the wrong type."
The top lines, node:internal/errors:540 and throw error;, show where Node.js threw the error internally. They are not part of your code, so they do not tell you the cause.
Find which line and which value is undefined
To find the cause, look just below the TypeError line. The first stack line, at Object.join (node:path:1354:7), shows that the error was raised by path.join. The next line, at Object.<anonymous> (/path/to/app/index.js:5:19), is where your code called path.join: line 5, column 19 of index.js.
Line 5 is const file = path.join(dataDir, 'message.txt');, so the value that was undefined is dataDir, that is, process.env.DATA_DIR. When an environment variable is not set, reading it from process.env gives undefined.
When I ran the same code as an ES module with the .mjs extension, the error line was the same. The first two lines changed to node:path:1354 and validateString(arg, 'path');, and your code's location appeared in the file:/// form, such as at file:///path/to/app/index.mjs:5:19.
Run with the environment variable set
If you only forgot to set the environment variable, you can fix it by setting it when you run the script.
$ DATA_DIR=./data node index.js
hello
Running export DATA_DIR=./data and then node index.js also printed hello.
If you set the variable and the error still happens, check the spelling of the name. Running DATADIR=./data node index.js without the underscore leaves DATA_DIR, which the code reads, unset. The same Received undefined error appeared.
Writing a .env file alone does not load it
Even when I placed a .env file containing DATA_DIR=./data in the same folder as index.js, running node index.js alone did not load it, and the same error appeared. Unless you specify otherwise, Node.js does not read .env files.
On Node.js 20.6.0 and later, you can specify the file to load with the --env-file option.
$ node --env-file=.env index.js
hello
When I specified a file that does not exist with --env-file, the command stopped before running the script (exit code 9).
$ node --env-file=missing.env index.js
node: missing.env: not found
--env-file-if-exists (added in Node.js 22.9.0) is for cases where you do not want to stop when the file is missing. If the file does not exist, it continues without reading anything. So if you misspell the file name, you do not get this option's error; you get the original Received undefined error.
Instead of passing an option, you can also call process.loadEnvFile() in your code to load the file (added in Node.js 21.7.0 and 20.12.0). If you omit the argument, it reads ./.env. Call it before the line that reads the environment variable.
const path = require('node:path');
const fs = require('node:fs');
process.loadEnvFile();
const dataDir = process.env.DATA_DIR;
const file = path.join(dataDir, 'message.txt');
console.log(fs.readFileSync(file, 'utf8').trim());
When I ran this file with node load-env.js, it printed hello without any error.
If an environment variable set in the terminal has the same name as one in .env, the terminal value is used. When I ran with DATA_DIR=/nonexistent and --env-file=.env and printed process.env.DATA_DIR, the output was /nonexistent. The official documentation also says that values already in the environment take precedence. If changing .env does not change the value, check whether an exported value is still set.
Check at startup and stop with a clearer message
Received undefined alone does not tell the reader which environment variable is missing. If you check the required environment variables before using them, and stop with your own message when one is missing, you will see the cause right away the next time it happens.
const path = require('node:path');
const fs = require('node:fs');
const dataDir = process.env.DATA_DIR;
if (!dataDir) {
console.error('環境変数 DATA_DIR が設定されていません。例: DATA_DIR=./data node checked.js');
process.exit(1);
}
const file = path.join(dataDir, 'message.txt');
console.log(fs.readFileSync(file, 'utf8').trim());
$ node checked.js
環境変数 DATA_DIR が設定されていません。例: DATA_DIR=./data node checked.js
Running with DATA_DIR=./data printed hello. I wrote if (!dataDir) instead of if (dataDir === undefined) so that it also stops for the empty string in the next section. When I ran with the value empty, as in DATA_DIR=, it also stopped with the same message.
An empty string does not cause this error
When you write only the name and leave the value empty, as in DATA_DIR=, process.env.DATA_DIR becomes an empty string, not undefined. An empty string is still a string, so path.join does not raise an error. As the official path.join documentation says, zero-length segments are ignored, so path.join('', 'message.txt') returned message.txt.
As a result, index.js read message.txt from the folder where I ran it, and stopped with a different error (the paths are replaced with example values).
$ DATA_DIR= node index.js
node:fs:440
return binding.readFileUtf8(path, stringToFlags(options.flag));
^
Error: ENOENT: no such file or directory, open 'message.txt'
at Object.readFileSync (node:fs:440:20)
at Object.<anonymous> (/path/to/app/index.js:6:16)
(I omitted the rest of the stack, since it has the same shape as the first error.)
Because this is an ENOENT error about a missing file, it is harder to notice that the environment variable is the cause. When I put a message.txt containing "other" in the folder where I ran the script and ran it the same way, it printed other without an error. If a file with the same name happens to exist, you can read the wrong file without noticing.
Keep this difference in mind when you want a default value. process.env.DATA_DIR ?? './data' uses ./data only when the value is undefined, and keeps an empty string as it is. process.env.DATA_DIR || './data' uses ./data for an empty string too. I printed the result of path.join(…, 'message.txt') for both expressions, and got the following.
| How it was run | Result of ?? |
Result of || |
|---|---|---|
DATA_DIR not set |
data/message.txt |
data/message.txt |
DATA_DIR= (empty) |
message.txt |
data/message.txt |
DATA_DIR=/srv/data |
/srv/data/message.txt |
/srv/data/message.txt |
The message varies with the function and the value
The same ERR_INVALID_ARG_TYPE error produced slightly different messages depending on the function and the value passed. Use these as a guide when you search or compare with your own error.
| Call | Message on the TypeError line |
|---|---|
path.join(undefined), path.dirname(undefined) |
The "path" argument must be of type string. Received undefined |
path.resolve(undefined) |
The "paths[0]" argument must be of type string. Received undefined |
path.resolve('a', undefined) |
The "paths[1]" argument must be of type string. Received undefined |
fs.readFileSync(undefined) |
The "path" argument must be of type string or an instance of Buffer or URL. Received undefined |
path.join(null) |
The "path" argument must be of type string. Received null |
path.join(8080) |
The "path" argument must be of type string. Received type number (8080) |
path.join({}) |
The "path" argument must be of type string. Received an instance of Object |
With path.resolve, the message shows which argument position was not a string, such as paths[1]. However, when I passed undefined as the second argument, as in path.join('a', undefined), the path.join message still said "path". In both cases, the first stack line (such as at Object.join) shows which function raised the error, and the following lines show where it was called in your code.
fs.readFileSync also accepts Buffers and URLs in addition to strings, so its message lists them too. When Received is followed by null or a number, suspect a value that came from somewhere other than an environment variable, such as a configuration file value or a function's return value.





