在laravel mix中需要一个npm包?

时间:2017-11-10 13:46:09

标签: laravel npm package laravel-mix

我已经看到这个问题在网上被回答了几次。但是,我只是没有让它工作。我希望你能提供帮助。

我正在使用Laravel 5.5和Laravel Mix。我没有手动将我需要的一些js文件加载到特定的目录中,而是想通过npm安装软件包,并在laravel mix中要求它们。

这适用于开箱即用的bootstrap。我将3.3.7版本替换为4.0.0 alpha6。

npm install bootstrap-v4-dev -D

然后在resources/assets/js/bootstrap.js我将其改为看起来像这样

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap-v4-dev');
} catch (e) {}

它有效。我可以运行npm run dev就好了。现在,我想用另一个npm包完成同样的事情。所以我npm install now-ui-kit -D我的package.json现在看起来像这样

{
  "private": true,
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "devDependencies": {
    "axios": "^0.16.2",
    "bootstrap-v4-dev": "^4.0.0-alpha.6",
    "cross-env": "^5.0.1",
    "jquery": "^3.1.1",
    "laravel-mix": "^1.0",
    "lodash": "^4.17.4",
    "now-ui-kit": "^1.0.1",
    "popper.js": "^1.11.1",
    "vue": "^2.1.10"
  },
  "dependencies": {
    "now-ui-kit": "^1.0.1"
  }
}

然后在bootstrap.js文件中添加require('now-ui-kit');,但是现在当我尝试编译时,我收到以下错误:

ERROR in ./resources/assets/js/bootstrap.js
Module not found: Error: Can't resolve 'now-ui-kit' in '/home/vagrant/code/user/resources/assets/js'
 @ ./resources/assets/js/bootstrap.js 15:0-21
 @ ./resources/assets/js/app.js
 @ multi ./resources/assets/js/app.js

我也尝试将require('now-ui-kit');放在app.js或甚至webpack.mix.js中。

没有任何效果,我真的不明白我做错了什么......有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

运行命令

npm install -D bootstrap@4.0.0-beta.3

然后使用以下代码

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}

答案 1 :(得分:0)

请按照以下步骤操作:

  • 安装npm软件包,然后安装jQuery npm i jquery
  • 创建一个.js的{​​{1}}文件
  • 现在打开resources/js/package.jswebpack.mix.js中添加package.js

    webpack.mix.js

将此内容写在mix.js([ 'resources/js/package.js', 'resources/js/otherJsFile.js' ], 'js/app.js').extract([ // Extract packages from node_modules to vendor.js 'jquery' ])中:

resources/js/package.js

最后在import $ from 'jquery'; 文件上使用js/vendor.jsjs/app.js

现在,您可以在从.blade导入的网站上使用jQuery