webpack-node-externals
webpack-node-externals copied to clipboard
not working with import statements
I can't seem to get this working with import statements. The modules are still bundled if I use import in my code. However, if I use require(), it works fine.
Am I missing something?
Please share some more insight
@prabal-raghav
I can confirm that.
I use import 'package' in my app and getModuleName function returns:
/home/hsz/Projects/project/src/package
instead of
package
Example input that is passed to the main function:
context /home/hsz/Projects/project/src/server/react
request /home/hsz/Projects/project/node_modules/newrelic
moduleName // empty string
When I have changed import 'newrelic' to require('newrelic') it gave me:
context /home/hsz/Projects/project/src/server/react
request newrelic
moduleName newrelic
I tried with setting includeAbsolutePaths: true as option, but when I deploy generated files to another instance paths are absolute and app cannot find modules (different dirs structure).
And this is what is present in output file:
module.exports = require("/home/hsz/Projects/project/node_modules/newrelic");
Workaround solution
In my webpack configuration instead of:
externals: [nodeExternals()]
use:
externals: [(context, request, callback) => {
const nodeModulesPath = path.resolve(__dirname, '../node_modules');
const fixedRequest = request.replace(`${nodeModulesPath}/`, '');
return nodeExternals()(context, fixedRequest, callback);
}],
I simply proxify the nodeExternals fixing path (removing unwanted absolute path prefix).
I have the same problem.
Hi coders,
I had the same issue and after some research, I've found a solution that works well for me.
import { nodeExternals } from "webpack-node-externals" 🚫
import nodeExternals from "webpack-node-externals" ✅
Happy coding 🖖