I am trying to master WebStorm. Created a project Node.js Express App.

I want to add a library.

Install it: $ npm install vsvg-paths

I understand that you need to connect it to app.js. I'm trying to do it like this:

var vsvg_paths = require("vsvg-paths");

app.use(vsvg_paths.decode());

But they tactfully tell me what I'm doing, something is wrong (says decode () is not a function or just a library connection error if I specify decode not as a function).

Tell me how to properly connect the library or where to read about it?

Here is the library itself https://github.com/jcblw/vsvg-paths

  • 3
    And what are you trying to get by calling app.use(vsvg_paths.decode()); ? - Pavel Mayorov
  • I'm trying to understand the overall structure of the project. Maybe I don’t understand how the libraries are connected. Therefore, I ask you to show on the example of this library how to do it right - Alexander Kasikov

2 answers 2

You connect it correctly. It is not clear how you want to use it and where. In the answer of Ares God, that's right, I rechecked.

Actions taken:

  • Created a project Node.js Express App ( WS version 2017.1.1, version of the application created by the express-generator - 4.15.5)
  • In the file app.js added the code Ares God:

 // этот ΠΈΠΌΠΏΠΎΡ€Ρ‚ Π²Π²Π΅Ρ€Ρ…Ρƒ Ρ„Π°ΠΉΠ»Π°, Π΄ΠΎ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ прилоТСния var paths = require( 'vsvg-paths' ); // этот ΠΊΠΎΠ΄ послС ΠΈΠΌΠΏΠΎΡ€Ρ‚ΠΎΠ² var path = 'M0 0 L10 10 l1 1'; var data = paths.decode(path); console.log('data:', data); 

  • The code brought the result to the console: Data: [ { x: 0, y: 0, rel: false }, { x: 10, y: 10, rel: false }, { x: 1, y: 1, rel: true } ]

Launched through the menu Run -> Run ... In the appeared window I chose app.js

If you wanted to use this library on the client, then you need to add it to the / public / javascript / vsvg-paths folder and import it into index.html

 <script src="public/javascripts/vsvg-paths/decode.js"></script> 

  • hmm .. what is the whole logic of the library execution should be in app.js? If this is so, then this is not true or I misunderstood you. I thought that having connected the library in app.js (I need the decode and encode functions), then I can use them in other scripts, for example in / public / js / script.js ... - Alexander Kasikov
  • Not necessarily, just the easiest way to check. The same code can be added to any route or controller (more experienced colleagues can fix me, if suddenly it is a bad practice) , but to run this code you will need to do more actions each time - Dan
  • Client code is stored in /public/js/script.js , that is, what will be transferred to the browser. If you need to use this library on the client (in the browser), then you need to import it completely differently. - Dan

Everything is written there:

 var paths = require( 'vsvg-paths' ); var path = 'M0 0 L10 10 l1 1'; var data = paths.decode(path); 
  • It does not work this way, that is, I cannot make var paths = require( 'vsvg-paths' ); in any script var paths = require( 'vsvg-paths' ); it worked (reauire not defined), you need to add it to the project somehow, and I don’t know how it is done in the Nodejs Express App - Alexander Kasikov
  • 2
    where you see require not defined - in the editor or in the console. when you start the application? In the first case, the problem is solved by connecting the Node.js Core library ( File | Settings | Languages ​​& Frameworks | Node.js and NPM ). In the second, the problem is how you run the code (maybe you are trying to execute it in the browser (?)?) - lena
  • I use WebStrom - I launch www.js and they tell me to go to localhost: [port number] - Alexander Kasikov