I need to pack a module I've written with webpack and transpile with babel. However I don't want to pack anothing from node_modules. Also, Node.js core modules shouldn't be packed.
I get these errors for core modules:
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'net' in '...project path...\node_modules\tunnel-agent'
@ ./node_modules/tunnel-agent/index.js 3:10-24
@ ./node_modules/request/lib/tunnel.js
@ ./node_modules/request/request.js
@ ./node_modules/request/index.js
So clearly, webpack is crawling node modules, that's something I don't want. It also clearly tries to load modules like net and fs.
How to stop this, and only pack and transpile my own code?
My config:
const path = require('path');
module.exports = {
entry: './serverless_function.js',
output: {
path: path.resolve('dist'),
filename: 'serverless_packed.js'
},
module: {
loaders: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ }
]
}
}
I need to pack my serverless function to be compatible with Node.js 6, which runs on Amazon AWS. It looks like this:
const Library = require("./MyLibrary");
const fs = require("fs");
async function serverlessFunction(event, context, callback) {
const response = {
statusCode: 200,
headers: {
"Content-Type": "image/png",
"Content-Disposition": "inline; filename=\"something.png\""
},
body: await Library.createImage().asBase64String("param1", "param2"),
isBase64Encoded: true
};
return callback(null, response);
}
module.exports = { serverlessFunction };
You can use the module webpack-node-externals to do exactly this:
const path = require('path');
const nodeExternals = require('webpack-node-externals');
module.exports = {
entry: './serverless_function.js',
output: {
path: path.resolve('dist'),
filename: 'serverless_packed.js'
},
// in order to ignore built-in modules like path, fs,
target: 'node',
// in order to ignore all modules in node_modules folder
externals: [nodeExternals()],
module: {
loaders: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ }
]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With