简体   繁体   中英

Questions about Node.js and downloading with npm

I'm having some trouble finding this specific information about NodeJS, I googled but I can't really find any answers to it. Hopefully you can clear some of my problems.

So, I installed NodeJS by following this guide in Ubuntu. I then used npm to install Sass by following this guide . I was able to get sass working just fine.

But here's where the confusion starts. I'm not entirely sure how to actually use Node. Am I supposed to always start a server by using this?

node myjsfile.js

If I don't do that and in my HTML file I add a script tag like so

<script src="js/myjsfile.js"></script>

Then when I load the page the console will output an error like

Uncaught ReferenceError: require is not defined

But, if I do run

node myjsfile.js

after setting up my files by following this guide , then none of the changes I do to the JS or HTML actually appear on my page, meaning that I have to constantly rerun the node command to see any updates.

I'm sure I'm missing something here but I can't figure out what. I'd really appreciate some help.

Require is not part of JavaScript. What is this Javascript "require"?

You have to transpile your file with a tool like grunt, webpack etc.

Node can interpret more than a browser can. This is why node does not throw an error. Also it is used server-side. If you want to execute your script in the browser, there is no need for node.

Node is for server side code (create a http server, doing some scripts, create a RestAPI).

Let's say you have created mynodejsfile.js. When you want to install a node package that will be used in this file. You reach the folder of mynodejsfile.js and:

npm install mypackage

This will install the dependency 'mypackage' in a node_modules folder beside 'mynodejsfile.js'.

Then you will be able to

require('mypackage')

in the 'mynodejsfile.js'.

If you want to create HTML content, you will node use your node file directly in the browser, node file are to be used with nodejs interpreter.

When you use html file with script you use a client side code, which will not be understood by your browser. When writing client code (for browser) you will use browser ECMA 5/6 code, and not nodejs code.

You can transpile your node file to make it usable in browser, but it is advanced/restricted use, and you should understand the difference between client/server code before going this way.

In my feeling you are mixing up behaviour of client side javascript functionality (like including a script file into a webpage) and building a NodeJS server. As Matthi pointed out, Node can interpret way more than the browser.

Regarding the necessity to constantly refresh your server instance I can hardly recommend nodemon to you. By default nodemon restarts your server after each code change, removing the problem of manually restarting the server in the development stage.

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