user3743266
user3743266

Reputation: 1172

How to import packages inside .vue files

In my .vue file script area, I have this:

<script>
    import get from 'lodash.get';

    ...
</script>

I'm trying to import lodash.get, but I get ReferenceError: get is not defined.

In my entry file(app.js which is a normal js file) it works and I could do

import get from 'lodash.get';
window.get = get;

But it would be better to have imports in each component.

Is there a way to import packages(lodash.get in my example) inside a vue component?

My folder structure:

node_modules
..lodash.get
....index.js
resources
..assets
....js
......components
........ComponentWhereINeedImport.vue
....app.js

None of these work:

import { get } from 'lodash/get';
import { get } from 'lodash.get';
import get from 'lodash/get';
import get from 'lodash.get';
require('../../../../node_modules/lodash.get/index.js');
require('../../../../node_modules/lodash.get/index');
require('../../../../node_modules/lodash.get');

Upvotes: 3

Views: 6189

Answers (1)

Marcus
Marcus

Reputation: 1848

Just write

<script>
import lodash from 'lodash';
.... 

Upvotes: 3

Related Questions