版本
我正在做一個相對較舊的 Laravel 7 專案。
npm -v
8.15.0
node -v
v14.17.1
問題
我使用 npm 安裝了一個新包
npm i my_new_package
現在我正在嘗試從我的 js 匯入包:
<script type="module" src="/js/filename.js"></script>
檔案名.js:
import { Roulette } from "my_new_package";
// Uncaught TypeError: Failed to resolve module specifier "@theblindhawk/roulette".
// Relative references must start with either "/", "./", or "../".
const Roulette = require("my_new_package");
// Uncaught ReferenceError: require is not defined
我從其他 SO 問題中嘗試了一堆東西,但似乎都不起作用。
當我嘗試在同一個存盤庫中創建檔案并呼叫它時沒有問題。
import { Roulette } from "./different_file.js";
我猜這與 CommonJS/ES6 有關,但我對這些知識非常缺乏。
uj5u.com熱心網友回復:
我搞砸了最基本的東西......
可能是因為我最近正在使用不同的檔案創建方法進行 Laravel 專案。
我忘了我必須將我的 js 檔案放在資源檔案夾中,然后從webpack.mix.js.
webpack 檔案:
mix.js('resources/js/filename.js', 'public/js')
然后運行npm run watch,它將為您在公用檔案夾中創建檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/514532.html
