NodeJS - 类型错误 [ERR_INVALID_ARG_TYPE]:“path”参数必须是字符串类型。收到未定义

问题描述 投票:0回答:6

我的 Webstorm 中有一个 Node/React 项目,由于此错误而无法运行。我必须重新安装 Windows 并重新开始我的开发。我将代码放回到 IDE 中,但是当我启动 Node 服务器时,出现以下错误:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

该错误的更多上下文:

[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)

我访问了index.js,这是第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),

我正在运行

npm run server
来启动我的节点服务器。

这是我的 package.json 如果有帮助的话:

{
  "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"
}
node.js babeljs package.json
6个回答
18
投票

好的,我解决了这个问题。我认为错误告诉我

path
未定义。事实上,它说传递给
path.join()
的变量未定义。那是因为我忘记将 .env 文件添加到根目录中,以便它可以获取这些变量。哎呀!


1
投票

就我而言,问题是缺少环境变量。我没有秘密。所以 SECRET 环境变量丢失了,我的测试试图访问它,结果它返回未定义

TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView. Received type undefined

1
投票

对我来说,Knex 配置文件有一个拼写错误。 为了测试我有

testing{
...

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

当我需要的时候

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

1
投票

我的问题是我在 env-cmd 命令之后添加了

&&
,这样我的启动脚本就得到了

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

而不是

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

当我删除

&&
时,一切都很好。


0
投票

这对我有用。 https://reactgo.com/typeerror-err-invalid-arg-type-react/

它说,当react-scripts版本较旧时会发生此错误。 因此,删除当前的所有节点模块,使用以下命令安装最新的反应脚本:“npm install react-scripts@latest”,然后安装所有其他依赖项。

我做了完全相同的事情,它成功了。


0
投票

这是完整的解决方案。您需要循环遍历文件才能获取所需的详细信息。

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}`);
});
© www.soinside.com 2019 - 2024. All rights reserved.