我正在尝试让我的Node项目与Webpack和Babel一起使用。当我尝试执行npm run server
时,我目前正在遇到此问题。
作为文字:
(node:13340) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'default' of undefined
at C:\Users\1silv\OneDrive\Documents\Senior Project\Sage\server.js:106608:67
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:13340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:13340) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:13340) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'default' of undefined
at C:\Users\1silv\OneDrive\Documents\Senior Project\Sage\server.js:106608:67
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:13340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
webpack built 0d416d43c5c5733e2a92 in 5978ms
× 「wdm」: Hash: 0d416d43c5c5733e2a92
Version: webpack 4.0.0
Time: 5978ms
Built at: 3/21/2019 4:11:26 PM
Asset Size Chunks Chunk Names
server.js 25.9 MiB app [emitted] [big] app
Entrypoint app [big] = server.js
[./node_modules/ejs/lib/ejs.js] 26 KiB {app} [built] [2 warnings]
[./node_modules/google-auth-library/build/src/index.js] 1.67 KiB {app} [built]
[./node_modules/mariadb/promise.js] 938 bytes {app} [built]
[./node_modules/moment/moment.js] 147 KiB {app} [built]
[./node_modules/plaid/lib/plaid.js] 492 bytes {app} [built]
[./node_modules/webpack-dev-middleware/index.js] 2.32 KiB {app} [built]
[./node_modules/webpack/lib/webpack.js] 7.71 KiB {app} [built]
[./server2.js] 26.3 KiB {app} [built]
[./webpack.config.js] 858 bytes {app} [built]
[child_process] external "child_process" 42 bytes {app} [built]
[fs] external "fs" 42 bytes {app} [built]
[http] external "http" 42 bytes {app} [built]
[https] external "https" 42 bytes {app} [built]
[util] external "util" 42 bytes {app} [built]
[0] multi ./server2.js 28 bytes {app} [built]
+ 1998 hidden modules
这是它引起错误的行:
/***/ "./node_modules/loader-runner/lib/loadLoader.js":
/*!******************************************************!*\
!*** ./node_modules/loader-runner/lib/loadLoader.js ***!
\******************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
var LoaderLoadingError = __webpack_require__(/*! ./LoaderLoadingError */ "./node_modules/loader-runner/lib/LoaderLoadingError.js");
module.exports = function loadLoader(loader, callback) {
if(true) {
__webpack_require__("./node_modules/loader-runner/lib lazy recursive")(loader.path).catch(callback).then(function(module) {
loader.normal = typeof module === "function" ? module : module.default; // <-----This is the line
loader.pitch = module.pitch;
loader.raw = module.raw;
if(typeof loader.normal !== "function" && typeof loader.pitch !== "function") {
return callback(new LoaderLoadingError(
"Module '" + loader.path + "' is not a loader (must have normal or pitch function)"
));
}
callback();
});
} else { var retry, module; }
};
运行npx webpack
仅会产生一系列警告,但是当我运行npm run server
时,它会给出相同的警告和一系列错误,如下所示:
所有../../
都使我想知道它是否正在尝试使用全局npm软件包,但是我想我已经摆脱了所有这些软件包。
它也不能编译我自己的.js
文件:
我可以确认文件index.js
在该文件夹中。
这是我的webpack.config.js
文件:
const path = require('path');
const webpack = require('webpack');
module.exports = {
target: 'node',
mode: 'development',
entry: {
app: ['./server2.js']
},
devtool: 'inline-source-map',
devServer: {
contentBase: './',
hot: true
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
output: {
filename: 'server.js',
path: path.resolve(__dirname, './'),
publicPath: '/'
},
module: {
rules: [
{
test: '/\.js$/',
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
}
这里是.babelrc
:
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": true
}
}
]
]
}
这是package.json:
{
"name": "sage-savings",
"version": "0.0.1",
"description": "An app for easy budgeting.",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch",
"build": "webpack --config webpack.config.js",
"server": "node server.js",
"start": "webpack-dev-server --open"
},
"author": "Dakota Dalton",
"repository": "https://github.com/1silvertiger/Sage",
"license": "ISC",
"dependencies": {
"@types/express": "^4.16.1",
"aws-sdk": "^2.422.0",
"body-parser": "^1.18.3",
"config": "^3.0.1",
"cors": "^2.8.5",
"ejs": "^2.6.1",
"express": "4.16.x",
"express-session": "^1.15.6",
"google-auth-library": "^3.1.0",
"mariadb": "^2.0.3",
"moment": "^2.24.0",
"mysql": "^2.16.0",
"plaid": "^2.15.0"
},
"devDependencies": {
"@babel/core": "^7.4.0",
"@babel/preset-env": "^7.4.1",
"ajv": "^6.10.0",
"babel-loader": "^8.0.0-beta.0",
"typescript": "^3.3.3333",
"webpack": "^4.0.0",
"webpack-cli": "^3.2.3",
"webpack-dev-middleware": "^3.6.1",
"webpack-hot-middleware": "^2.24.3"
}
}
目前,我不确定是什么原因导致了错误。
编辑:我运行npm install -D babel-loader @babel/core @babel/preset/env webpack
重新安装了所有软件包,并在loaderLoader.js
软件包中按this commit在loader-runner
中注释了有问题的代码。顶部的错误现在消失了,但是底部的所有错误仍然存在。到目前为止,服务器运行良好。我有一个repo on GitHub,它将重现该错误。尝试npm run build
,然后尝试npm run server
。