Winston: Webpack Error: Can't resolve 'async/forEach'

Created on 24 May 2018  ยท  7Comments  ยท  Source: winstonjs/winston

my environment:

  • winston 3.0.0-rc5
  • node 6.7.0
  • OS Linux
  • ES6/7 with webpack+babel

What is the problem?

When using webpack to bundle my server code I get an error while bundling when using winston:

ERROR in ../node_modules/winston/lib/winston/exception-handler.js
Module not found: Error: Can't resolve 'async/forEach' in '~/myProject/node_modules/winston/lib/winston'
resolve 'async/forEach' in '~/myProject/node_modules/winston/lib/winston'
    Parsed request is a module
    using description file: ~/myProject/node_modules/winston/package.json (relative path: ./lib/winston)
    after using description file: ~/myProject/node_modules/winston/package.json (relative path: ./lib/winston)
    resolve as module
        looking for modules in ~/myProject/node_modules
        using description file: ~/myProject/package.json (relative path: ./node_modules)
        after using description file: ~/myProject/package.json (relative path: ./node_modules)
            using description file: ~/myProject/node_modules/async/package.json (relative path: ./forEach)
            no extension
                ~/myProject/node_modules/async/forEach doesn't exist
            .js
                ~/myProject/node_modules/async/forEach.js doesn't exist
            .json
                ~/myProject/node_modules/async/forEach.json doesn't exist
            as directory
                ~/myProject/node_modules/async/forEach doesn't exist

my code:

import winston from "winston";
[...]

opening a node console and using CommonJS require does not lead to any error,
so I suppose it's a combination of winston and webpack.

my webpack config looks like this:

const ServerConfig = {
    context: distPath,
    entry: ["babel-polyfill", path.resolve(srcPath, "src", "server")],
    output: {
        path: distPath,
        filename: "server.bundle.js"
    },
    profile: true,
    stats: outputOptions,
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loaders: ["happypack/loader?id=js"]
            }
        ]
    },
    target: "node",
    plugins: [
        new webpack.DefinePlugin({ "global.GENTLY": false }),
        new CopyWebpackPlugin([
            {
                context: srcPath,
                from: "config",
                to: "config"
            }
        ]),
        new HappyPack({
            id: "js",
            loaders: [
                {
                    loader: "babel-loader",
                    options: {
                        cacheDirectory: true
                    }
                }
            ]
        }),
        /**
         * The settings below fix warnings due to require path handling. Switching context to packages
         * base folder solves some warnings. Colors package dynamically requires themes, which we'll
         * assign the default located at the packages themes folder.
         */
        new webpack.ContextReplacementPlugin(/colors[\/\\]lib$/, "../themes/generic-logging.js")
    ],
    resolve: {
        modules: [path.resolve(__dirname, "node_modules")]
    },
    externals: nodeModules
};

What do you expect to happen instead?

Well ...

Other information

It looks like winston is trying to require something, that is not part of the async package.

Most helpful comment

removing the resolve statement in the webpack config solved my issue, thanks! :+1:

I still wonder why this wasn't an issue with all my other modules.

PS.
Adding "node_modules" to the array fixed the problem as well.

resolve: {
    modules: [path.resolve(__dirname, "node_modules"), "node_modules"]
}

All 7 comments

What is the output of npm ls -l --depth=2 for your package? If you could also provide the dependencies and devDependencies from your package.json that would be helpful. I suspect npm may be installing an older version of async in your tree somehow (e.g. if you explicitly depend on async@1).

โ”œโ”€โ”ฌ [email protected] 
โ”‚ โ”œโ”€โ”€ [email protected] 
โ”‚ โ”œโ”€โ”ฌ [email protected] 
โ”‚ โ”‚ โ”œโ”€โ”ฌ [email protected] 
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”ฌ [email protected] 
โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ [email protected] 
โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [email protected] 
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [email protected] 
โ”‚ โ”‚ โ”œโ”€โ”ฌ [email protected] 
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [email protected] 
โ”‚ โ”‚ โ””โ”€โ”ฌ [email protected] 
โ”‚ โ”‚   โ””โ”€โ”€ [email protected] 
โ”‚ โ”œโ”€โ”ฌ [email protected] 
โ”‚ โ”‚ โ”œโ”€โ”€ [email protected] 
โ”‚ โ”‚ โ””โ”€โ”€ [email protected] 
โ”‚ โ”œโ”€โ”€ [email protected] 
โ”‚ โ”œโ”€โ”€ [email protected] 
โ”‚ โ”œโ”€โ”€ [email protected] 
โ”‚ โ””โ”€โ”€ [email protected] 

npm list async:

โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”€ [email protected] 
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”€ [email protected] 
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”€ [email protected] 
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ””โ”€โ”ฌ [email protected]
โ”‚     โ””โ”€โ”€ [email protected] 
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”€ [email protected] 
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ””โ”€โ”€ [email protected] 
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ””โ”€โ”€ [email protected] 
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ””โ”€โ”€ [email protected] 
โ””โ”€โ”ฌ [email protected]
  โ””โ”€โ”€ [email protected] 
Colors package dynamically requires themes, which we'll assign the default located at the packages themes folder.

Not anymore, colors removed the dynamic requires :) Don't think this affects the current issue but you may not need that extra plugin in your webpack config.

I do see some older async's in your tree, though the ones under winston look good. The problem is it looks like it's searching for the module only in ~/myProject/node_modules, not under e.g. ~/myProject/node_modules/winston/node_modules, so it may be picking up an old version not the 2.x that should come under winston. This could be related to the resolve block you have in your config?

In any case feels like a webpack config issue not a winston issue... :)

removing the resolve statement in the webpack config solved my issue, thanks! :+1:

I still wonder why this wasn't an issue with all my other modules.

PS.
Adding "node_modules" to the array fixed the problem as well.

resolve: {
    modules: [path.resolve(__dirname, "node_modules"), "node_modules"]
}

Please fix that problem. I had the same issue.

As Nikita suggested above, it seems that this is a webpack config issue, not a winston issue. If you can prove otherwise, feel free to let us know. Thanks!

This worked for me:

npm uninstall async
npm install -g async
npm link async
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sinai-doron picture sinai-doron  ยท  3Comments

alditis picture alditis  ยท  3Comments

Buzut picture Buzut  ยท  3Comments

anks333 picture anks333  ยท  3Comments

amiram picture amiram  ยท  4Comments