How To Configure Babel Correctly To Use Lodash-es?
I need to use lodash-es in my project, but I can't configure my babel correctly, it always reports errors like SyntaxError: Unexpected identifier hello.js import upperCase from 'lo
Solution 1:
Adapted from https://stackoverflow.com/a/31822668/3563013
require("@babel/register")({
ignore: [/node_modules\/(?!lodash-es)/],
});
Solution 2:
babel-node
by default ignores the node_modules
directory. Which is a good thing, else it would be unnecessarily heavy. Packages in node_modules
are (at the moment) expected to be in commonjs
format. Instead of using lodash-es
(es6 format) you should just use lodash
(commonjs format). It has exactly the same functionality, the only difference is the format it is written in. More information about that here.
So either tweak babel-node
to unignore node-modules/lodash-es
(not recommended!) or just install lodash
with npm install --save lodash
and then rewrite your import like:
import upperCase from'lodash/upperCase'// instead of lodash-esconsole.log(upperCase('lodash-es'));
Post a Comment for "How To Configure Babel Correctly To Use Lodash-es?"