any-promise
any-promise copied to clipboard
Module parse failed: /.../node_modules/any-promise/LICENSE Unexpected token
Hi,
I am building an isomorphic project (code that can run in browser and in node) using webpack, and I am currently facing an weird issue and webpack does not give me enough data to understand what is going on (see stack trace below).
From what I know of webpack, it complains here that there is a dynamic require() (eg: require(var)
) so it try to resolve every possible path, and then it breaks on files like LICENSE, README etc...
Warning: %s Critical dependencies:
22:12-28 the request of a dependency is an expression
Warning: %s Module parse failed: /Users/moox/Sync/Development/putaindecode.io/node_modules/any-promise/LICENSE Unexpected token (1:14)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (1:14)
at Parser.pp$4.raise (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp.unexpected (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:603:10)
at Parser.pp.semicolon (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:581:61)
at Parser.pp$1.parseExpressionStatement (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:966:10)
at Parser.pp$1.parseStatement (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:730:24)
at Parser.pp$1.parseTopLevel (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:638:25)
at Parser.parse (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:516:17)
at Object.parse (/Users/moox/Sync/Development/putaindecode.io/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/Users/moox/Sync/Development/putaindecode.io/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/Users/moox/Sync/Development/putaindecode.io/node_modules/webpack/lib/NormalModule.js:104:16) +1ms
Why are you using dynamic require? I have a workaround (to use webpack "externals" option) but this is causing me others issues as well, so I would like to know if we can do something over here to avoid this issue in the first place.
Hello,
This may be related to #23 with a workaround in #24 , but I'm not sure why it's indicating trying to parse the LICENSE file. That's really odd. (just realized you answered this above)
Why are you using dynamic require?
It's to preserve backwards compatibility with the behavior of old Node.js versions and "basic registration". The browser field in package.json
overrides register.js
with register-shim.js
and contains no dynamic require. This worked fine with basic webpack tests, but I am definitely more familiar with browserify at this point.
Is it possible to force webpack
to use the browser build of any-promise
and then apply the patch in #24 in your situation? You would lose the "auto discover" behavior, but that only applies to older Node.js versions and never worked in the browser, so it really doesn't apply to your situation.
The thing is: my webpack build is not used in the browser. I have 2 entries: one for the browser, one for node, and both are compiled to be able to use webpack loaders for CSS etc.
I see. Does webpack provide a way to check a global/env to eliminate code blocks in a way that wouldn't break browserify or vanilla node setups? If so, we could probably just eliminate the dynamic require blocks and most things would work fine.
Does webpack provide a way to check a global/env to eliminate code blocks in a way that wouldn't break browserify or vanilla node setups?
Not sure. webpack is supposed to handle dynamic require, but will try to bundle everything that might be in the path it is detected to be the folder where files can be required. That's why it's globbing some files like LICENSE.
@kevinbeaty I'm running into this same issue. (webpack + dynamic require trying to require the entire directory) -- I'm not sure how to use the workaround in #24 (am I missing something, like a way to trigger the webpack specific build in my package?) -- although I also may be having a separate issue. In my situation, I am using electron, so I have a target: electron in my webpack config.
Here's a quick workaround:
module.exports = {
plugins: [
new webpack.NormalModuleReplacementPlugin(/^any-promise$/, 'bluebird'),
],
}
What this does is it simply replaces all requires/imports of any-promise
with your choice of Promise implementation, e.g. bluebird
in my case. If you want to use the native Promise, try some simple polyfill, e.g. pinkie-promise
(its index.js
is module.exports = typeof Promise === 'function' ? Promise : require('pinkie')
), I think Webpack will eliminate the polyfill itself (don't take my word on it though).
@jeremejevs For the "simple polyfill" part you can try promise-monofill - a simple module that I wrote that always returns the native promise, with no polyfills. It should be even simpler than pinkie.