Converting Web Scripts to Local

Overview


When you use a module that is used in the browser, you may have to convert the file to be useable on your local machine. In particular, for security reasons, Node.js was designed to load modules off the local machine, as opposed to loading modules that are retrieved from a web site.

Converting Node js Libraries


In Node.js, the following is an illegal statement, because Node will not allow you to load libraries from a web site.


import * as la from 'http://server.com/lib/linear-algebra/v1.0.0/linear-algebra.mjs';
					


Rather, if the linear algebra library is located at '/lib/linear-algebra/v1.0.0/linear-algebra.mjs' on the local machine, the following script will work.


import * as la from '/lib/linear-algebra/v1.0.0/linear-algebra.mjs';
					


Note, some modules have module dependencies. That is, they will have their own import statements. Those statements must also refer to libraries on the local machine using the syntax as above.

Local Program with Local Dependencies


The following shows a functioning local script. The imported modules are loaded inside an async function, and the path is specified relative to the root of the local machine. If this is a windows machine, the library would be located at C:/lib/linear-algebra/v1.0.0/matrix.mjs.


async function test(){
  let la = await import('/lib/linear-algebra/v1.0.0/matrix.mjs');
  //do something here
}

test();
					


Contents