简体   繁体   中英

What is the best practice to reference JavaScript libraries that when installed via NPM (in the node_modules folder)?

For example, I created folder, opened it with VS Code. Opened a terminal, used the command npm install jquery , it creates a folder node_modules within my created folder. I then create an HTML file, put HTML template in and now I want to reference jQuery. I know how to reference the files syntactically and all that, I am just looking to understand best practices.

Do I just reference jQuery... JavaScript files within there? Or am I missing a step?

before starting on a new project you should use npm init . this will initialize the project and generate a package.json file. 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.

example after initializing the npm:

  npm i jquery

and you can get it using

const jquery = require('jquery')

or

import jquery from 'jquery';

ie; without using the relative path, example:

import jquery from '../../node_modules/jquery..'

It sounds like you are missing the build step.

When developing with npm, you usually use a toolchain with compilers, bundlers and minifiers, which read the libraries from the node_modules folder and put the resources in a build folder which actually gets deployed, and from where the js file will be loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM