bluzky / nice-select2

A lightweight vanilla javascript library that replaces native select elements with customizable dropdowns
https://bluzky.github.io/nice-select2/
MIT License
361 stars 60 forks source link

Issue with importing package through ES6 notation #57

Open AleAtSoftplace opened 1 year ago

AleAtSoftplace commented 1 year ago

Hello, I've installed the package through NPM and I'm trying to import it in my project, but when I try to

import * as ns from "nice-select2"
window.NiceSelect = ns;

I get a "Package is not installed" error. The package is present in the node_modules directory.

My stack is Vite v4.3.8 and Tailwind 3.3.2 This is the full error log, I think something in the manifest or exports could be wrong.

9:57:06 AM [vite] Internal server error: Failed to resolve entry for package "nice-select2". The package may have incorrect main/module/exports specified in its package.json. Plugin: vite:import-analysis File: /var/www/html/resources/js/app.js at packageEntryFailure (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:23382:11) at resolvePackageEntry (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:23379:5) at tryNodeResolve (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:23113:20) at Context.resolveId (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:22874:28) at async Object.resolveId (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:42847:32) at async TransformContext.resolve (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:42575:23) at async normalizeUrl (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:40500:34) at async file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:40651:47 at async Promise.all (index 7) at async TransformContext.transform (file:///var/www/html/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:40577:13)

maykbrito commented 1 year ago

Add this line to this module's package.json node_modules/nice-select2/package.json

"module": "dist/js/nice-select2.js",
justin-oh commented 1 year ago

I'm also running into an issue with the module not being recognized through the syntax import NiceSelect from 'nice-select2';.

I'm using Symfony's Webpack Encore and receiving this error:

Module build failed: Module not found:
"./assets/backend/backend.js" contains a reference to the file "nice-select2".
This file can not be found, please check it for typos or update it if the file got moved.

My work-around was to reference the file directly, based on my project's directory structure:

import NiceSelect from '../../node_modules/nice-select2/src/js/nice-select2.js';