Split the file with nodejs.Understanding the Possible Function Calls Without Module Reference

Asked 2 months ago, Updated 2 months ago, 3 views

We are considering making the code described in javaScript Node js.

Nodejs typically use require() to refer to functions categorized by files in modular form.

For the above correspondence, all functions of the complexly invoked Base application must be given a module name.

Is it possible to access the files in the above configuration without the module name?

node.js

2022-09-30 11:35

1 Answers

The easiest way to access a split file without a module name is to make it one file before running it in Node.js.

If you are using HTML as follows,

<script src="script1.js"></script>
<script src="script2.js"></script>
<script src="script3.js"></script>

In the npm script, you can combine them into one file as follows:

concat-oscript.js./script1.js./script2.js./script3.js

Also, in nodo.js10, import/export on ES6 is Experimental, but it works.For example,

my-func.mjs

export function my-func1(){
    console.log ("Hello World 1")
}

export function my-func2(){
    console.log ("Hello World 2" )
}

index.mjs

import {my-func1, my-func2}from "./my-func";
my-func1();
my-func2();

As , when you run it, add --experimental-modules to the options.

node--experimental-modules index.mjs

The import/export details for ES6 can be found on the following page of the MDN:
MDN import export


2022-09-30 11:35

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.