我使用commonjs规范定义了两个模块
a.js文件
exports.aaaFn = () => {
console.log('*****************************');
console.log('我是a模块');
console.log('*******************************');
};
b.js文件
const aFn = require('./a');
console.log(aFn.aaaFn());
使用webpack按照commonjs规范打包,运行后报错。webpack.config.js如下
const path = require('path');
module.exports = {
entry: './js/b.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
libraryTarget: 'commonjs'
},
mode: 'development'
};
在浏览器运行的时候报如下错误
求大佬知道下这是怎么回事
libraryTarget: 'commonjs'
把这句删掉就行了
这句的作用是把打包的文件以commonjs的规范导出,如果在浏览器运行不需要写这个