NodeJS - TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义
Posted
技术标签:
【中文标题】NodeJS - TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义【英文标题】:NodeJS - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined 【发布时间】:2020-12-30 05:31:02 【问题描述】:我的 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_modules 和 package-lock.json 并执行了npm i
并重新运行了服务器,结果相同。
【参考方案1】:
好的,我找到了问题所在。我以为错误告诉我path
未定义。事实上,它说传递给path.join()
的变量是未定义的。那是因为我忘记将我的 .env 文件添加到根目录,以便它可以获取这些变量。哎呀!
【讨论】:
【参考方案2】:这对我有用。 https://reactgo.com/typeerror-err-invalid-arg-type-react/
它说,当 react-scripts 版本旧时会发生此错误。 因此,删除所有当前节点模块,使用以下命令安装最新的 react-scripts:'npm install react-scripts@latest',然后安装所有其他依赖项。
我做了完全相同的事情,它奏效了。
【讨论】:
【参考方案3】:在我的例子中,问题是缺少一个 env 变量。我没有秘密。所以 SECRET 环境变量丢失了,我的测试试图访问它,结果它返回 undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be one of type string, TypedArray, or DataView. Received type undefined
【讨论】:
【参考方案4】:对我来说,Knex 配置文件有错字。 为了测试我有
testing
...
migrations:
connection
directory: './src/db/migrations',
tableName: 'knex_migrations',
,
...
当我需要时
testing
...
migrations:
directory: './src/db/migrations',
tableName: 'knex_migrations',
,
...
【讨论】:
以上是关于NodeJS - TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义的主要内容,如果未能解决你的问题,请参考以下文章
如何解决 NodeJS 中的“TypeError:将循环结构转换为 JSON”
NodeJS Mongoose record.save TypeError
TypeError:无法读取未定义的nodejs请求http的属性'0'
nodejs-dialogflow 库返回 TypeError: dialogflow.SessionsClient 不是构造函数