liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 62 forks source link

change location of package.json #96

Closed its-dibo closed 3 years ago

its-dibo commented 3 years ago

this package assumes that the file package.json is located inside process.cwd() which is not a must. the location of this file must be relative to the current file (i.e: webpack.config.js) by default which is more expected.

https://github.com/liady/webpack-node-externals/blob/6dc8265e24788b70add4e2bf50fd1a13581d18af/utils.js#L53

use case:

if you use a single package.json file to control multiple projects (monorepo), and each file has it's own package.json file contains it's own scripts, but not dependencies.

if you want to run build for any project from the workspace's root, rather than the project's root using npm --prefix flag

npm run build:server:dev --prefix projects/example

in this case process.cwd() refers to projects/example which doesn't contain the dependencies field in it's package.json file, but it is located in the workspace's root (i.e: ../../package.json). it is more easily to refer to the correct package.json path relative to the current webpack configuration file, so it is fixed regardless of the current working directory.

even using options.filename doesn't help:

  config.externals.push(
    nodeExternals({
      modulesFromFile: true,
      fileName: path.join(__dirname, "./package.json")
    })
  );

__dirname here refers to the directory where webpack.config.js is located.

liady commented 3 years ago

@eng-dibo note that you need to specify it like so:

  config.externals.push(
    nodeExternals({
      modulesFromFile: {
        fileName: path.join(__dirname, "./package.json")
      }
    })
  );
liady commented 3 years ago

Closing this as it should be resolved with the correct config structure, please let me know if it doesn't work