Home > front end >  How to stop webpack compiling AWS libraries in node_modules?
How to stop webpack compiling AWS libraries in node_modules?

Time:02-21

The AWS compilation errors from within node_modules are occurring the build

ERROR in ../../../node_modules/@aws-sdk/client-dynamodb/models/models_0.ts 6321:51-59
TS2339: Property '$unknown' does not exist on type 'never'.
6319 |     if (obj.NULL !== undefined) return { NULL: obj.NULL };
6320 |     if (obj.BOOL !== undefined) return { BOOL: obj.BOOL };
> 6321 |     if (obj.$unknown !== undefined) return { [obj.$unknown[0]]: "UNKNOWN" };
     |                                                   ^^^^^^^^
6322 |   };
6323 | }
6324 |

webpack compiled with 9 errors in 6844 ms

I have just updated AWS

 "@aws-sdk/client-dynamodb": "^3.51.0",
 "@aws-sdk/lib-dynamodb": "^3.51.0",
 "@aws-sdk/util-dynamodb": "^3.51.0",

With webpack

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

/*
 This line is only required if you are specifying `TS_NODE_PROJECT` for whatever 
 reason.
 */
// delete process.env.TS_NODE_PROJECT;

module.exports = {
  context: __dirname,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'source-map',
  resolve: {
    extensions: ['.mjs', '.json', '.ts'],
    symlinks: false,
    cacheWithContext: false,
    plugins: [
        new TsconfigPathsPlugin({
            configFile: './tsconfig.paths.json',
        }),
    ],
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  target: 'node',
  externals: [nodeExternals()],
  module: {
    rules: [
        {
            // Include ts, tsx, js, and jsx files.
            test: /\.(ts|js)x?$/,
            exclude: [
                path.resolve(__dirname, 'node_modules'),
                path.resolve(__dirname, '.serverless'),
                path.resolve(__dirname, '.webpack'),
            ],
            use: [
                {
                    loader: 'ts-loader',
                    options: {
                        transpileOnly: true,
                    }
                },
                'babel-loader'
            ]
        }
    ]
},
plugins: [new ForkTsCheckerWebpackPlugin()],

};

And ts-config

{
"extends": "./tsconfig.paths.json",
"compilerOptions": {
  "lib": ["ESNext"],
  "moduleResolution": "node",
  "esModuleInterop": true,
  "noUnusedParameters": false,
  "noUnusedLocals": false,
  "resolveJsonModule": true,
  "removeComments": true,
  "sourceMap": true,
  "target": "ES2020",
  "outDir": "lib",
  "paths": {
    "@functions/*": ["src/functions/*"],
    "@libs/*": ["src/libs/*"],
    "@api/*": ["src/api/*"],
    "~/*": [
    "./*"
    ]
  },
  "types": [
    "@types/node",
    "@types/jest"
  ]
},
"include": ["src/**/*.ts", "serverless.ts"],
"exclude": [
  "util/**/*",
  "node_modules/**/*",
  ".serverless/**/*",
  ".webpack/**/*",
  "_warmup/**/*",
  ".vscode/**/*"     ],
"ts-node": {
  "require": ["tsconfig-paths/register"]
}

}

Webpack looks to be still compiling node_modules regardless of it being in the excluded list.

CodePudding user response:

Stop typescript type lookup traversing parent nodes

Incompatible types found by going up directories

Alter tsconfig.json to include typeRoots as a parameter as per Prevent require(...) from looking up modules in the parent directory

{
"extends": "./tsconfig.paths.json",
"compilerOptions": {
  "lib": ["ESNext"],
  "allowSyntheticDefaultImports": true,
  "moduleResolution": "node",
  "module": "commonjs",
  "esModuleInterop": true,
  "noUnusedParameters": false,
  "noUnusedLocals": false,
  "resolveJsonModule": true,
  "removeComments": true,
  "sourceMap": true,
  "target": "ES2020",
  "outDir": "lib",
  "paths": {
    "@functions/*": ["src/functions/*"],
    "@libs/*": ["src/libs/*"],
    "@api/*": ["src/api/*"],
      "~/*": [
      "./*"
    ]
  },
  "typeRoots": [
    "./"
  ],
  "types": [
    "@types/node",
    "@types/jest"
  ]
},
"include": ["src/**/*.ts", "serverless.ts"],
"exclude": [
  "util/**/*",
  "node_modules",
  "node_modules/**/*",
  ".serverless/**/*",
  ".webpack/**/*",
  "_warmup/**/*",
  ".vscode/**/*"
],
"ts-node": {
  "require": ["tsconfig-paths/register"]
}
}
  • Related