我在学习nodejs的时候在慕课网上发现了一个课程比较有意思,从js到ts开发数独游戏。在课程中遇到了一个比较奇怪的问题,经过查询后发现是版本和老师的不一致导致,故几经百度问问等查询,解决了。
第一个问题,遇到的是输出时名称乱码,是因为gulpfile.js中配置
gulp.task(“webpack”,()=>{
//转移js
const webpack = require(“webpack-stream”);
const config = require(“./webpack_config.js”);
gulp.src(“./js/*/.js”).pipe(webpack(config)).pipe(gulp.dest(“../www/js”));
});
webpack(config);我写成了webpack()。相当于没有加载配置。
第二个问题,加载好config后,按老师代码配置写的:
module.exports = {
entry:{
index:"./js/index.js"
},
output:{
filename: "[name].js"
},
devtool:"source-map",
resolve:{
extensions:[".js"]
},
module:{
loader:[
{
test:/\.js$/,
loader:"babel-loader",
exclude:"node_modules",
query:{
presets:["es2015"]
}
}
]
}
}
查询后说版本问题,报 WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.module has an unknown property ‘loader’. These properties are valid:
等等的错
是要把loader:[ 换成rules:[ 。
然后报WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.module.rules[0].exclude: The provided value “node_modules” is not an absolute path!
-> A rule
这是说该路径不是一个绝对路径,故换成
exclude: [
path.resolve(__dirname, “node_modules”)
]
然后注册const path = require(‘path’);
就可以成功转译了。