What is the correct way to use a package installed by npm? [duplicate]

2

To explain my question I will use jquery as an example. I installed jquery from npm (npm install jquery). Then npm created the folder node_modules \ jquery \ dist where is the jquery file that I need (jquery.js).

To use this file should I point to this node_modules folder? That is, if the folder of my project is at the same level as the folder node_modules should I do this to use my js?:

<script src="../node_modules/jquery/dist/jquery.js">

I created an .js file in both the folder of my project and the same folder in the folder node_modules and tried to import it as follows:

var $ = require('jquery')

but it does not work. What would be the right way to do this?

    
asked by anonymous 19.11.2016 / 22:26

2 answers

2

I particularly like to use NPM for server dependencies and Bower for client dependencies.

To install Bower, simply use the command:

npm i bower -g

After doing this you can create a file named .bowerrc as described here with the location for you to install the dependencies.

How to use is similar to NPM, so you can save dependencies with --save and stuff like that.

EDIT 1

Considering the article npm and front-end packaging you can create another package.json within your public folder for your dependencies of front-end , but this is not recommended.

    
19.11.2016 / 22:43
0

The way you loaded jQuery in the first example was to work except that you did not close the <script> tag, maybe that's the problem?

Functional example:

<!DOCTYPE html>
<html lang="pt-br">
<head>
  <meta charset="UTF-8">
  <title>Testando jQuery</title>
</head>
<body>

  <script src="./node_modules/jquery/dist/jquery.js"></script>

  <script>
    $(window).ready(function () {
      alert("Testando jQuery");
    });
  </script>

</body>
</html>
    
20.11.2016 / 04:37