You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目集成text-mask遇Javascript导入失败问题求助

Fixing "Import not found" for createNumberMask in Laravel

Hey there, let's work through that "Import not found" error you're facing when trying to use createNumberMask from text-mask-addons in your Laravel project. Here are the most common fixes to get your price input mask up and running:

1. Verify the package is installed correctly

First, make sure you've actually installed both text-mask and text-mask-addons (since addons depend on the core text-mask library). Run this command in your project root:

npm install text-mask text-mask-addons --save
# Or if you use yarn:
yarn add text-mask text-mask-addons

After installing, restart your Laravel Mix watcher (npm run watch) or recompile your assets (npm run dev) to ensure the packages are included in your build.

2. Adjust your import path

The official docs' import path might not work depending on your project's module resolution setup. Try these alternative import paths:

  • Simplified import (check if the package exposes the addon directly):
    import createNumberMask from 'text-mask-addons';
    
  • Target the source file directly (if the dist version is causing issues):
    import createNumberMask from 'text-mask-addons/src/createNumberMask';
    
  • Use CommonJS require as a fallback (useful if ES modules aren't being handled properly):
    const createNumberMask = require('text-mask-addons/dist/createNumberMask').default;
    
    The .default is important here because some ES module packages export their main functionality as a default export, which requires this extra step when using require.

3. Check your Laravel Mix configuration

Ensure your webpack.mix.js is set up to handle ES modules correctly. If you're using Babel, make sure it's not excluding the text-mask-addons package from transpilation. For most standard Laravel setups, the default Mix config should work, but if you've customized it, double-check that node_modules packages are being processed properly.

4. Example working implementation

Once the import is fixed, here's how to set up a price mask with common configurations:

import createNumberMask from 'text-mask-addons/dist/createNumberMask';

// Configure the mask for price input (2 decimal places, thousands separator)
const priceMask = createNumberMask({
  prefix: '',
  suffix: '',
  includeThousandsSeparator: true,
  thousandsSeparatorSymbol: ',',
  allowDecimal: true,
  decimalSymbol: '.',
  decimalLimit: 2,
  allowNegative: false,
  allowLeadingZeroes: false
});

// Apply the mask to your input (example with vanilla JS)
// import vanillaTextMask from 'text-mask/dist/vanillaTextMask';
// vanillaTextMask({ inputElement: document.getElementById('price-input'), mask: priceMask });

If you're still stuck, check the node_modules/text-mask-addons directory to confirm the file structure matches the path you're using. Sometimes package updates change file locations, so verifying the actual files exist can help pinpoint the issue.

内容的提问来源于stack exchange,提问作者user7301337

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:59:13