Hello i'm using Luxon in react app with react-scripts. Everting works ok on dev server (react-scripts start) but when i try to build / compile app with react-scripts build this error occurs:
$ react-scripts build
Creating an optimized production build...
Failed to compile.
Failed to minify the code from this file:
./node_modules/luxon/src/impl/util.js:49
Read more here: http://bit.ly/2tRViJ9
is there anything what can i do to fix this?
relevant versions
"react": "16.4.1",
"react-dom": "16.4.1",
"react-scripts": "1.1.4",
"luxon": "1.3.1",
How are you including Luxon in your project? Luxon's package.json points at the compiled code, which you want to use. You're pointing react-scripts at the source files, which are failing, presumably, because react-scripts is not running Babel, or at least not Babel with the right set of plugins.
In general, you can't expect raw source code to be consumed by an arbitrary toolchain. But don't worry, I did the work for you! You just have to use the build artifact.
i have dependency in package.json and i'm using it in project from this package in flow code
import {DateTime} from 'luxon';
const now = DateTime.utc();
Hmm, I guess the issue must be that the module field points at src: https://github.com/moment/luxon/blob/master/package.json#L59. I'll need to think about that.
I'm just tested it. When i use
import {DateTime, Duration, Interval} from '../../node_modules/luxon/build/cjs-browser/luxon';
// instead of
import {DateTime, Duration, Interval} from 'luxon';
it works OK
Make sense. I'm going to leave this open, since I need to rework the module entry in package.json anyway
I'm having a similar problem, but I've always used "luxon/build/cjs-browser/luxon.js" and it's now causing errors:
preferring built-in module 'util' over local alternative at 'C:\develop\web\node_modules\util\util.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
...
No name was provided for external module 'util' in options.globals – guessing 'util'
Creating a browser bundle that depends on Node.js built-in module ('util'). You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins
Note that this is not in a React app, and I'm using Rollup.
However, it looks like the browser build - ostensibly safe for use in a browser - depends on a Node library.
Edit 1:
Failing version is 1.3.1. The cjs-browser/luxon.js file contains a require('util') in that version.
In version 1.3.0, cjs-browser/luxon.js does not contain any require()s at all, and works fine.
Edit 2:
I can open a new issue if you think this is different enough. The reason I replied here is because this feels like it's all related to the various ways to require the library and use the different bundles.
I'm getting a similar problem when using luxon 1.3.1 with Expo. It works with 1.3.0.
The package at "node_modules/luxon/build/cjs-browser/luxon.js" attempted to import the Node standard library module "util". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo
Same here...
It fails because of this code: https://github.com/moment/luxon/blob/2b65af4a58b91039934628961b468ef803f939e6/src/impl/util.js#L208-L212
(and to clarify, the same code is in build/cjs-browser/luxon.js.
this try-catch block would correctly catch an error in require('util') _at runtime_, but if you use any kind of code bundler - webpack or React Native's Metro - the require call is handled _at the time of packaging_ - and the try-catch does nothing to prevent a packager error.
It's quite hard to make a build that would work in any packager. Issues with bundlers and pre-compiled NPM modules are very common.
The best course of action is to have the application's packager process the raw source code of luxon, and use some kind of packager guard to remove the require from a browser build.
I've now removed uses of util altogether
Most helpful comment
I'm getting a similar problem when using luxon 1.3.1 with Expo. It works with 1.3.0.
The package at "node_modules/luxon/build/cjs-browser/luxon.js" attempted to import the Node standard library module "util". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo