webpack: Module not found: Error: Can't resolve 'aws-sdk' when importing Webpack

Bug report

What is the current behavior?

Importing Webpack into an Express application results in errors being thrown.

If the current behavior is a bug, please provide the steps to reproduce.

webpack.config.dev.js:

const path = require('path');

const server = {
    entry: './index.js',
    mode: "production",
    target: 'node',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].bundle.js',
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: "babel-loader"
            }, {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                use: "babel-loader"
            }
        ]
    }
};

module.exports = [server];

developmentMode.js

import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';

export default (app) => {
    const webpackConfig = require('./webpack.config.dev')[0];

    const compiler = webpack(webpackConfig);

    app.use(webpackDevMiddleware(compiler, {}));
}

server.js

import express from 'express';
import ReactDOMServer from 'react-dom/server';
import React from 'react';

import developmentMode from './developmentMode';

import HelloWorld from './src/HelloWorld.jsx';

const app = express();
const port = 3000;

if (process.env.NODE_ENV === "dev") developmentMode(app);

const template = application => `
<html>
    <head>
    </head>
    <body>
        ${application}
    </body>
</body>
`;

const application = ReactDOMServer.renderToString(<HelloWorld />);

app.get('/', (req, res) => res.send(template(application)));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

package.json

{
  "version": "6.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config webpack.config.dev.js",
    "start": "node dist/main.bundle.js"
  },
  "repository": {
    "type": "git",
  },
  "author": "Jacob Clark",
  "license": "MIT",
  "dependencies": {
    "express": "^4.16.4",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-redux": "^5.1.1",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.5",
    "@babel/core": "^7.1.6",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.4",
    "webpack": "^4.26.0",
    "webpack-cli": "^3.1.2",
    "webpack-dev-middleware": "^3.4.0"
  }
}

Running webpack --config webpack.config.dev.js yields the following errors

[clarkj84:~/Desktop/jacobclark.xyz]$ npm run build && npm start                                                                                                                                                                 (6.0.0-dev✱) 

> jacobclarkxyz@6.0.0 build /Users/clarkj84/Desktop/jacobclark.xyz
> webpack --config webpack.config.dev.js

Hash: b1078bef90371617b277
Version: webpack 4.26.0
Child
    Hash: b1078bef90371617b277
    Time: 33472ms
    Built at: 21/11/2018 08:01:36
     1 asset
    Entrypoint main = main.bundle.js
       [1] external "path" 42 bytes {0} [built]
       [9] (webpack)/lib/Template.js 7.96 KiB {0} [built]
      [13] external "events" 42 bytes {0} [built]
      [39] (webpack)/lib/dependencies/DependencyReference.js 1.75 KiB {0} [built]
      [45] (webpack)/lib/util/createHash.js 1.92 KiB {0} [built]
      [46] (webpack)/lib/Module.js 10.1 KiB {0} [built]
      [53] (webpack)/lib/ModuleFilenameHelpers.js 5.36 KiB {0} [built]
      [60] (webpack)/lib/Dependency.js 1.97 KiB {0} [built]
      [79] (webpack)/lib/ExternalsPlugin.js 546 bytes {0} [built]
      [87] (webpack)/lib/Stats.js 38.2 KiB {0} [built]
     [110] (webpack)/lib/node/NodeTemplatePlugin.js 880 bytes {0} [built]
     [513] (webpack)/lib/webpack.js 7.71 KiB {0} [built]
     [515] (webpack)-dev-middleware/index.js 2.21 KiB {0} [built]
    [1325] ./webpack.config.dev.js 433 bytes {0} [built]
    [1327] ./index.js + 2 modules 1.13 KiB {0} [built]
           | ./index.js 743 bytes [built]
           | ./developmentMode.js 273 bytes [built]
           | ./src/HelloWorld.jsx 118 bytes [built]
        + 1313 hidden modules
    
    WARNING in ./node_modules/terser-webpack-plugin/dist/worker.js 14:130-137
    Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
     @ ./node_modules/terser-webpack-plugin/dist/TaskRunner.js
     @ ./node_modules/terser-webpack-plugin/dist/index.js
     @ ./node_modules/terser-webpack-plugin/dist/cjs.js
     @ (webpack)/lib/WebpackOptionsDefaulter.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/fsevents/fsevents.js 14:13-71
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/worker-farm/lib/child/index.js 49:33-53
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/worker-farm/lib/fork.js
     @ ./node_modules/worker-farm/lib/farm.js
     @ ./node_modules/worker-farm/lib/index.js
     @ ./node_modules/terser-webpack-plugin/dist/TaskRunner.js
     @ ./node_modules/terser-webpack-plugin/dist/index.js
     @ ./node_modules/terser-webpack-plugin/dist/cjs.js
     @ (webpack)/lib/WebpackOptionsDefaulter.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/util/versioning.js 17:20-67
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib sync ^\.\/.*$
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/loader-runner/lib/loadLoader.js 3:2-28
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/loader-runner/lib/LoaderRunner.js
     @ (webpack)/lib/NormalModule.js
     @ (webpack)/lib/AutomaticPrefetchPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/pre-binding.js 20:22-48
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/express/lib/view.js 81:13-25
    Critical dependency: the request of a dependency is an expression
     @ ./node_modules/express/lib/application.js
     @ ./node_modules/express/lib/express.js
     @ ./node_modules/express/index.js
     @ ./index.js
    
    WARNING in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js
    Module not found: Error: Can't resolve 'node-gyp' in '/Users/clarkj84/Desktop/jacobclark.xyz/node_modules/fsevents/node_modules/node-pre-gyp/lib/util'
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib sync ^\.\/.*$
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js
    Module not found: Error: Can't resolve 'npm' in '/Users/clarkj84/Desktop/jacobclark.xyz/node_modules/fsevents/node_modules/node-pre-gyp/lib/util'
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib sync ^\.\/.*$
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/util/nw-pre-gyp/index.html 1:0
    Module parse failed: Unexpected token (1:0)
    You may need an appropriate loader to handle this file type.
    > <!doctype html>
    | <html>
    | <head>
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib sync ^\.\/.*$ ./util/nw-pre-gyp/index.html
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    WARNING in ./node_modules/loader-runner/lib/loadLoader.js 3:2-28
    System.import() is deprecated and will be removed soon. Use import() instead.
    For more info visit https://webpack.js.org/guides/code-splitting/
     @ ./node_modules/loader-runner/lib/LoaderRunner.js 7:17-40
     @ (webpack)/lib/NormalModule.js
     @ (webpack)/lib/AutomaticPrefetchPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    ERROR in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/unpublish.js
    Module not found: Error: Can't resolve 'aws-sdk' in '/Users/clarkj84/Desktop/jacobclark.xyz/node_modules/fsevents/node_modules/node-pre-gyp/lib'
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/unpublish.js 16:14-32
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib sync ^\.\/.*$
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    ERROR in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/publish.js
    Module not found: Error: Can't resolve 'aws-sdk' in '/Users/clarkj84/Desktop/jacobclark.xyz/node_modules/fsevents/node_modules/node-pre-gyp/lib'
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/publish.js 18:14-32
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib sync ^\.\/.*$
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
    
    ERROR in ./node_modules/fsevents/node_modules/node-pre-gyp/lib/info.js
    Module not found: Error: Can't resolve 'aws-sdk' in '/Users/clarkj84/Desktop/jacobclark.xyz/node_modules/fsevents/node_modules/node-pre-gyp/lib'
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/info.js 14:14-32
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib sync ^\.\/.*$
     @ ./node_modules/fsevents/node_modules/node-pre-gyp/lib/node-pre-gyp.js
     @ ./node_modules/fsevents/fsevents.js
     @ ./node_modules/chokidar/lib/fsevents-handler.js
     @ ./node_modules/chokidar/index.js
     @ ./node_modules/watchpack/lib/DirectoryWatcher.js
     @ ./node_modules/watchpack/lib/watcherManager.js
     @ ./node_modules/watchpack/lib/watchpack.js
     @ (webpack)/lib/node/NodeWatchFileSystem.js
     @ (webpack)/lib/node/NodeEnvironmentPlugin.js
     @ (webpack)/lib/webpack.js
     @ ./developmentMode.js
     @ ./index.js
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! jacobclarkxyz@6.0.0 build: `webpack --config webpack.config.dev.js`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the jacobclarkxyz@6.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/clarkj84/.npm/_logs/2018-11-21T08_01_36_419Z-debug.log

What is the expected behavior?

Webpack should run without errors.

Other relevant information: webpack version: 4.26.0 Node.js version: 9.4.0 Operating System: OS X 10.12.6 Additional tools: N/A

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 40
  • Comments: 20 (1 by maintainers)

Most upvoted comments

I solved this using https://github.com/liady/webpack-node-externals and forcing webpack to ignore all node_modules

I’m having the same problem as soon as I added a dependency for sqlite3. Not sure how that’s related, but my whole app works fine, but as soon as I import { Database } from 'sqlite3' I get this error. Note: node-pre-gyp was already present before adding this dependency.

It seems the proposed solution in this thread is to use the webpack-node-externals package.

I tried that, and it did work. However, I later found out that I cannot use webpack-node-externals in my project. I am trying to create a script and run it in a browser; webpack-node-externals essentially tells webpack not to bundle node_modules as dependencies. However, since my target environment is in the browser, I NEED those node_modules packaged into my script as dependencies. So this issue is still unresolved for me.

How can I fix this error, WITHOUT using webpack-node-externals???

Just use nodeExternals exactly as it’s documented in their readme @imVinayPandya

I’m having the same problem as soon as I added a dependency for sqlite3. Not sure how that’s related, but my whole app works fine, but as soon as I import { Database } from 'sqlite3' I get this error. Note: node-pre-gyp was already present before adding this dependency.

Hey, I am facing the same error after adding SQLite in my electron react app. Could you please share - how did you fixed this? thank you

Hey, I too have the same problem after I tried adding sqlite3 to my electron react app. It would be really helpful if someone shares the fix.

@samkelleher hey! how do you use nodeExternals with next.config.js? I am not getting it to work. Do you push it onto the config or what?

I’m having the same problem as soon as I added a dependency for sqlite3. Not sure how that’s related, but my whole app works fine, but as soon as I import { Database } from 'sqlite3' I get this error. Note: node-pre-gyp was already present before adding this dependency.

Hey, I am facing the same error after adding SQLite in my electron react app. Could you please share - how did you fixed this? thank you

I’m having the same error. I’m trying to make webpack HMR work with Express and Pug and it’s a nightmare. And now this aws-sdk error.

I’m having the same problem as soon as I added a dependency for sqlite3. Not sure how that’s related, but my whole app works fine, but as soon as I import { Database } from 'sqlite3' I get this error. Note: node-pre-gyp was already present before adding this dependency.

Hey, I am facing the same error after adding SQLite in my electron react app. Could you please share - how did you fixed this? thank you

Hey, I too have the same problem after I tried adding sqlite3 to my electron react app. It would be really helpful if someone shares the fix.

me too, can anyone help me?

Webpack is adding to compilation every import/require it seems. When there is used require as an expression (that should executed for example only conditionally), then it will try to bundle it too. That’s why for example ‘knex’ has a lot of problems with webpack, because it has a lot of dependencies being required canditionally during runtime.

My solution for this is to use externals for everything that is to be bundled by webpack, but unneeded.

Example configuration for ‘knex’ and ‘sqlite3’ that also drops aws-sdk:

externals: { 'aws-sdk': 'aws-sdk', pg: 'pg', 'pg-query-stream': 'pg-query-stream', mssql: 'mssql', 'mssql/lib/base': 'mssql/lib/base', 'mssql/package.json': 'mssql/package.json', mysql: 'mysql', mysql2: 'mysql2', oracle: 'oracle', oracledb: 'oracledb', tedious: 'tedious' }