在Laravel 5.7.10中,我安装了一些节点模块,但是似乎无法弄清楚如何使它们可用于客户端JS。以OpenLayers为例,这些是我遵循的步骤:
npm install ol --save
npm install
npm run dev
我的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": "npm run development -- --watch",
"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 --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.18",
"bootstrap": "^4.0.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^2.0",
"lodash": "^4.17.5",
"parcel-bundler": "^1.12.2",
"vue": "^2.5.7",
"ol": "^5.3.1",
"ol-popup": "^4.0.0",
"popper.js": "^1.14.7"
},
"dependencies": {
"axios": "^0.18",
"bootstrap": "^4.0.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^2.0",
"lodash": "^4.17.5",
"parcel-bundler": "^1.12.2",
"vue": "^2.5.7",
"ol": "^5.3.1",
"ol-popup": "^4.0.0",
"popper.js": "^1.14.7"
}
}
我在resources / js / app.js中拥有这个:
/* OpenLayers dependencies */
const pbf = require('pbf');
const pixelworks = require('pixelworks');
const rbush = require('rbush');
require('ol');
在public / js / app.js中,我可以看到OpenLayers核心代码已加载到其中。
我的php页面末尾有以下内容:
<script src="{!! asset('js/app.js') !!}"></script>
在我正在运行的客户端JS中,我使用以下函数检查是否存在模块:
function moduleAvailable(name) {
try {
require.resolve(name);
return true;
} catch(e){}
return false;
}
function checkForModule(module_name) {
if (moduleAvailable(module_name)) {
console.log("Module '"+module_name+"' loaded via Mix!");
} else {
console.log("Module '"+module_name+"' not detected");
}
}
当我使用'ol'作为module_name运行上面的函数时,我总是得到“未检测到模块'ol'”-在package.json中列为dev-dependencies的所有其他模块也是如此。 / p>
我忍不住想想我确实缺少了一些简单的东西,但是经过几天的努力,我还是无法破解。我还应该检查什么?