webpack-node-externals icon indicating copy to clipboard operation
webpack-node-externals copied to clipboard

not working with import statements

Open calvinl opened this issue 8 years ago • 4 comments

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?

calvinl avatar Sep 09 '17 00:09 calvinl

Please share some more insight

prabal-raghav avatar Sep 27 '17 15:09 prabal-raghav

@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).

hsz avatar Sep 28 '17 10:09 hsz

I have the same problem.

liana-p avatar Sep 06 '18 10:09 liana-p

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 🖖

DYW972 avatar Feb 09 '22 16:02 DYW972