liady / webpack-node-externals

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

Error including local npm module #93

Open janoist1 opened 3 years ago

janoist1 commented 3 years ago

I'm getting errors when using local npm package:

...
"common": "file:../common",
...
Serverless: Packing external modules: aws-sdk@^2.270.1, uuid@^8.3.2, ramda@^0.27.1, common@file:../common, atob@^2.1.2, es6-promisify@^6.1.1, btoa@^1.2.1, node-forge@^0.10.0, slugify@^1.4.6, node-ses@^3.0.3, request-promise-native@^1.0.7, request
Serverless: WARNING: Could not determine version of module request

  Error --------------------------------------------------

  Error: ENOENT: no such file or directory, stat '/Users/i/Dev/name-of-project/api/.webpack/dependencies/node_modules/common'

I'm pulling my hair as Serverless ecosystem keeps throwing 'random' errors at me. This is the most recent one blocking me.

Serverless: 1.83.2 "copy-webpack-plugin": "^6.1.1", "serverless-webpack": "^5.3.5", "webpack": "^4.44.2", "webpack-node-externals": "^2.5.2"

serverless.yml:

  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

webpack.config:

const slsw = require("serverless-webpack")
const nodeExternals = require("webpack-node-externals")
// const CopyPlugin = require("copy-webpack-plugin")

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  // Generate sourcemaps for proper error messages
  devtool: 'source-map',
  // Since 'aws-sdk' is not compatible with webpack,
  // we exclude all node dependencies
  externals: [nodeExternals()],
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  optimization: {
    // We do not want to minimize our code.
    minimize: false
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false
  },
  // node: false,
  // devtool: 'inline-cheap-module-source-map',
  // Run babel on all .js files and skip those in node_modules
  module: {
    rules: [
      {
        test: /\.js$/,
        include: __dirname,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  {
                    targets: { node: '12' },
                    useBuiltIns: 'usage',
                    corejs: 3,
                  },
                ],
              ],
            },
          },
        ],
      },
    ],
  },
  plugins: [
    // TODO
    // new CopyPlugin([
      // 'path/to/specific/file',
      // 'recursive/directory/**',
    // ]),
  ],
};