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

时间:2020-09-11 16:59:05

标签: node.js babeljs package.json

我的Webstorm中有一个Node / React项目,由于该错误而无法运行。我必须重新安装Windows并重新开始开发。我将代码重新输入到IDE中,但是当我启动节点服务器时,出现以下错误: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"
}

2 个答案:

答案 0 :(得分:0)

好的,我知道了问题所在。我以为错误告诉我path未定义。事实上,这是说传递给path.join()的变量是未定义的。那是因为我忘记将.env文件添加到根目录中,因此它可以捕获这些变量。哎呀!

答案 1 :(得分:-2)

更新 package.json 文件中的所有 npm。它对我有用。