Home > other >  How to import lodash into a JavaScript file
How to import lodash into a JavaScript file

Time:02-02

After installing packages e.g. lodash using npm install --save lodash, I am trying to add it to the top of the file.

import _ from 'lodash';

console.log(add(10, 3));

When I start Live Server from VS Code, I get this error. I cannot figure it out how to fix it? Please help me:

Uncaught TypeError: Failed to resolve module specifier "lodash". Relative references must start with either "/", "./", or "../".

I am trying to write the path in different ways, but it did not help me.

CodePudding user response:

You can call any method by using _. Example:

console.log(_.add(10, 3));

CodePudding user response:

Try this instead of _

import lodash from 'lodash';

This tells webpack to resolve from node_modules as the relative path is not mentioned.

Assumption: You are already using webpack to bundle your packages.

  • Related