Reputation: 10077
I used webpack to pack a couple files using this config.
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
entry: ['./jstasklib/task.js', './jstasklib/worker.js'],
output: {
path: __dirname + '/dist',
filename: 'app.js',
library: 'jstasklib'
},
target: 'node',
externals: [nodeExternals()],
devtool: 'source-map',
module: {
loaders: [
{
test: /.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015'],
},
},
],
},
};
If i then start a node terminal and require the file, it just gives me an empty object:
node
var foo = require('./dist/app.js');
console.log(foo);
-> {}
However, if I toss a console.log(jstasklib)
at the bottom of app.js and the run node ./dist/app.js
I get this output which seems like it should allow me to at least import Worker in that earlier example:
{default: [Function: Worker]}
So what gives? Is my webpack config wrong? Whats the proper setup to be able to import things from the webpack generated file?
Upvotes: 4
Views: 933
Reputation: 128
Maybe you're using export default in your app.js file, this isn't set by default. Try setting the output libraryTarget to commonjs2
https://webpack.github.io/docs/configuration.html#output-librarytarget
output: {
path: __dirname + '/dist',
filename: 'app.js',
library: 'jstasklib',
libraryTarget: 'commonjs2'
},
Upvotes: 2