babel having trouble with import statement
I don't think you need to exclude the node_modules
in your loader config. However, you might want to let webpack know what to resolve. Try adding something like this:
resolve: { root: path.join(__dirname), fallback: path.join(__dirname, 'node_modules'), modulesDirectories: ['node_modules'], extensions: ['', '.json', '.js', '.jsx', '.scss', '.png', '.jpg', '.jpeg', '.gif']},
The modulesDirectories
key should keep webpack from running down every single require / import in your working directory.
Also, adding target to the top of your config should resolve issues with builtins like fs
target: 'node'
Ok I figured it out thanks to other answers and 4m1r' answer. I post the example code.
var path = require('path');var fs = require('fs');var nodeModules = {};fs.readdirSync('node_modules') .filter(function (x) { return ['.bin'].indexOf(x) === -1; }) .forEach(function (mod) { nodeModules[mod] = 'commonjs ' + mod; });module.exports = { name: 'server', target: 'node', context: path.join(__dirname, 'src', 'cloud'), entry: { server: './main.js' }, output: { path: path.join(__dirname), filename: '[name].js' }, externals: nodeModules, module: { loaders: [ {test: /\.js$/, exclude: /node_modules/, loaders: ['babel-loader?presets[]=es2015']} ] }, resolve: { root: path.join(__dirname), fallback: path.join(__dirname, 'node_modules'), modulesDirectories: ['node_modules'], }};
What was really important too was the externals key which prevented it fro leaking to the node_modules through requires and specifying for some reason ?presets[]=2015
in the babel-loader. I'm accepting 4m1r because it was what finally fixed the code.