![](/img/trans.png)
[英]Node npm why all the modules are installed in the root node_modules folder?
[英]What is the best practice to reference JavaScript libraries that when installed via NPM (in the node_modules folder)?
例如,我創建了文件夾,用 VS Code 打開它。 打開一個終端,使用命令npm install jquery
,它在我創建的文件夾中創建了一個文件夾 node_modules 。 然后我創建一個 HTML 文件,將 HTML 模板放入,現在我想引用 jQuery。 我知道如何在語法上引用文件以及所有這些,我只是想了解最佳實踐。
我是否只是在其中引用 jQuery... JavaScript 文件? 還是我錯過了一步?
在開始一個新項目之前,您應該使用npm init
。 這將初始化項目並生成package.json
文件。 This package.json
file is used for managing the project's dependencies(libraries), scripts, version etc. And after this every time you install a library using npm it will get listed in this package.json
file under dependencies section.
初始化 npm 后的示例:
npm i jquery
你可以使用它
const jquery = require('jquery')
或者
import jquery from 'jquery';
IE; 不使用相對路徑,例如:
import jquery from '../../node_modules/jquery..'
聽起來您缺少構建步驟。
使用 npm 進行開發時,通常使用帶有編譯器、捆綁器和壓縮器的工具鏈,它們從node_modules
文件夾中讀取庫並將資源放入實際部署的build
文件夾中,並從該文件夾中加載 js 文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.