NodeJS - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
Asked Answered
S

7

15

I have a Node/React project in my Webstorm that won't run because of this error. I had to reinstall Windows and start fresh with my development. I got the code back into the IDE, but when I start up the Node server, I am getting the following error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

More context for that error:

[nodemon] starting `babel-node src/node-server/index.js`
internal/validators.js:122
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at validateString (internal/validators.js:122:11)
    at Object.join (path.js:375:7)
    at Object.<anonymous> (C:\Projects\Production-Orchestrator\src\node-server\/index.js:17:15)

I went to index.js and here is lines 1-17:

// npm run server
import dotenv from 'dotenv';
import express from 'express';
import path from 'path';
import sql from 'mssql';
import cors from 'cors';
import http from 'http';
import { setupWebSocket } from './ws/setupWebSocket.js';

const useWebSockets = true;

dotenv.config();

const dbConfig = {
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    server: path.join(process.env.DB_SERVER, process.env.DB_HOST),

I am running npm run server to start up my node server.

And here is my package.json if it helps:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "description": "my app",
  "homepage": ".",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/xxxxxxx"
  },
  "license": "UNLICENSED",
  "scripts": {
    "buildp": "env-cmd -f .env.production react-scripts build",
    "buildt": "env-cmd -f .env.test react-scripts build",
    "buildw": "webpack --config ./webpack.config.js --mode production",
    "eject": "react-scripts eject",
    "eslint-check": "eslint --print-config src/components/search/Search.js | eslint-config-prettier-check",
    "server": "nodemon --exec babel-node src/node-server/index.js",
    "start": "SET REACT_APP_WS_PORT=3001 & react-scripts start",
    "startw": "webpack-dev-server --config ./webpack.config.js --mode development --open",
    "stylelint": "stylelint **/*.scss",
    "test": "react-scripts test",
    "ws": "node --experimental-modules src/node-server/websocket.js",
    "ws2": "nodemon --exec babel-node src/node-server/websocket.js"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "dependencies": {
    "@babel/preset-react": "^7.8.0",
    "@emotion/core": "latest",
    "@fortawesome/fontawesome": "^1.1.8",
    "@fortawesome/fontawesome-svg-core": "^1.2.27",
    "@fortawesome/free-regular-svg-icons": "^5.12.1",
    "@fortawesome/free-solid-svg-icons": "^5.12.1",
    "@fortawesome/react-fontawesome": "^0.1.8",
    "@popperjs/core": "^2.0.6",
    "@react-pdf/renderer": "^1.6.8",
    "@types/react": "^16.9.19",
    "animate.css": "^3.7.2",
    "axios": "^0.19.2",
    "babel-loader": "^8.0.6",
    "bootstrap": "^4.4.1",
    "bufferutil": "^4.0.1",
    "cors": "^2.8.5",
    "dayjs": "^1.8.20",
    "device-detector-js": "^2.2.1",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-ws": "^4.0.0",
    "file-saver": "^2.0.2",
    "javascript-time-ago": "latest",
    "jquery": "^3.4.1",
    "jsbarcode": "^3.11.0",
    "mssql": "^6.1.0",
    "popper.js": "^1.16.1",
    "print-js": "^1.0.63",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-animations": "^1.0.0",
    "react-beautiful-dnd": "^12.2.0",
    "react-beforeunload": "^2.2.1",
    "react-collapse": "^5.0.1",
    "react-confirm-alert": "^2.6.1",
    "react-custom-scrollbars": "^4.2.1",
    "react-detect-offline": "^2.4.0",
    "react-dnd": "^10.0.2",
    "react-dnd-html5-backend": "^10.0.2",
    "react-dom": "^16.12.0",
    "react-modal": "^3.11.1",
    "react-notifications-component": "^2.3.0",
    "react-popup": "^0.10.0",
    "react-radio-group": "^3.0.3",
    "react-router-dom": "^5.1.2",
    "react-scripts": "^3.4.3",
    "react-select": "^3.0.8",
    "react-spinners": "^0.8.0",
    "react-spring": "^8.0.27",
    "react-time-ago": "^5.0.7",
    "react-transition-group": "^4.3.0",
    "sort-package-json": "^1.40.0",
    "styled-components": "^4.4.1",
    "typescript": "^3.7.5",
    "utf-8-validate": "^5.0.2",
    "ws": "^7.2.5"
  },
  "devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.8.4",
    "@babel/node": "^7.10.5",
    "@babel/preset-env": "^7.8.4",
    "@welldone-software/why-did-you-render": "^4.2.5",
    "css-loader": "^3.4.2",
    "dotenv-cli": "^3.2.0",
    "env-cmd": "^10.1.0",
    "eslint": "^6.8.0",
    "eslint-config-prettier": "^6.10.0",
    "eslint-loader": "^3.0.3",
    "eslint-plugin-babel": "^5.3.0",
    "eslint-plugin-prettier": "^3.1.2",
    "file-loader": "^5.0.2",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "node-sass": "^4.13.1",
    "nodemon": "^2.0.4",
    "prettier": "^1.19.1",
    "react-hot-loader": "^4.12.19",
    "sass-loader": "^8.0.2",
    "stylelint": "^13.2.0",
    "stylelint-config-rational-order": "^0.1.2",
    "stylelint-config-standard": "^19.0.0",
    "stylelint-order": "^4.0.0",
    "stylelint-scss": "^3.14.2",
    "url-loader": "^3.0.0",
    "webpack": "^4.44.1",
    "webpack-cli": "^3.3.11"
  },
  "proxy": "http://localhost:3000"
}
Sexy answered 11/9, 2020 at 16:59 Comment(2)
I hope you tried installing modules first then try to run the server?Naiad
I deleted node_modules and the package-lock.json and did a npm i and reran the server with the same results.Sexy
S
20

OK, I figured out the issue. I thought the error was telling me that path was undefined. When it fact it was saying the variables passed into path.join() were undefined. And that was because I forgot to add in my .env file to the root so it could grab those variables. Whoops!

Sexy answered 11/9, 2020 at 22:23 Comment(0)
L
1

In my case, the issue was the missing of an env variable. I did not have SECRET. So the SECRET env variable was missing and my test was trying to access it and as a result it was returning undefined

TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView. Received type undefined
Lassa answered 16/12, 2021 at 17:32 Comment(0)
H
1

For me was the Knex configuration file had a typo. For testing I had

testing{
...

    migrations: {
        connection{
              directory: './src/db/migrations',
              tableName: 'knex_migrations',
        }    
    },
...
}

when I needed

testing{
    ...
    
    migrations: {
          directory: './src/db/migrations',
          tableName: 'knex_migrations',
    },
...
}
Hostler answered 20/12, 2021 at 12:2 Comment(0)
D
1

My issue was that I added && after the env-cmd command, so that for my start script I got

"start": "env-cmd -e development && react-scripts start",

instead of

"start": "env-cmd -e development react-scripts start",

When I removed the && everything was fine.

Disney answered 28/2, 2022 at 21:39 Comment(0)
O
1

Here is the full solution. You need to loop through files to get required details.

const http = require('http');
const formidable = require('formidable');
const fs = require('fs');
const path = require('path');

const server = http.createServer((req, res) => {
  if (req.url === '/upload' && req.method.toLowerCase() === 'post') {
    const form = new formidable.IncomingForm();

    // Specify the directory where uploaded files will be stored
    form.uploadDir = path.join(__dirname, 'uploads');

    form.parse(req, (err, fields, files) => {
      if (err) {
        console.error(err);
        return;
      }
      var originalFilename,oldPath;
      console.log(files)
      const uploadedFiles = files.file;
      
      uploadedFiles.forEach((uploadedFile) => {
      originalFilename= uploadedFile.originalFilename;
      oldPath=uploadedFile.filepath;
        console.log(originalFilename);    
      });
      const newPath = path.join(form.uploadDir, originalFilename);

      fs.rename(oldPath, newPath, (err) => {
        if (err) {
          console.error(err);
          return;
        }

        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('File uploaded successfully');
      });
    });
  } else {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(`
      <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit" value="Upload File">
      </form>
    `);
  }
});

const port = 3000;
server.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});
Orose answered 17/9, 2023 at 15:34 Comment(1)
This helps to solve my problem. Thank you so much.Seibold
D
0

This worked for me. https://reactgo.com/typeerror-err-invalid-arg-type-react/

It says, that this error occurs when the react-scripts version is old. So, delete all your current node modules, install the latest react-scripts using: 'npm install react-scripts@latest' and then install all the other dependencies.

I did the exact same thing, it worked.

Deirdra answered 25/8, 2021 at 17:26 Comment(0)
C
0

In my case, the issue occurred when migrating an old project from node 14 to node 18. Upgrading the packages individually did not work.

Removing yarn.lock file and running yarn install seemed to work.

If you're using npm then delete package.json.lock and run npm install

Cellini answered 6/6, 2024 at 9:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.