简体   繁体   English

有关Node.js以及使用npm下载的问题

[英]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. 我在Google上找到有关NodeJS的特定信息时遇到了一些麻烦,但是我找不到真正的答案。 Hopefully you can clear some of my problems. 希望您能解决我的一些问题。

So, I installed NodeJS by following this guide in Ubuntu. 因此,我按照Ubuntu中的本指南安装了NodeJS。 I then used npm to install Sass by following this guide . 然后,我按照本指南使用npm安装Sass。 I was able to get sass working just fine. 我能够使Sass正常工作。

But here's where the confusion starts. 但是,这就是混乱的开始。 I'm not entirely sure how to actually use Node. 我不完全确定如何实际使用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 如果不这样做,则在HTML文件中添加一个脚本标签,如下所示

<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. 按照本指南设置文件后,对JS或HTML所做的任何更改实际上都不会显示在我的页面上,这意味着我必须不断地重新运行node命令以查看任何更新。

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. Require不是JavaScript的一部分。 What is this Javascript "require"? 这个Javascript“要求”是什么?

You have to transpile your file with a tool like grunt, webpack etc. 您必须使用grunt,webpack等工具转换文件。

Node can interpret more than a browser can. Node的解释能力超过浏览器。 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). 节点用于服务器端代码(创建http服务器,执行一些脚本,创建RestAPI)。

Let's say you have created mynodejsfile.js. 假设您已经创建了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: 您到达mynodejsfile.js文件夹,并:

npm install mypackage

This will install the dependency 'mypackage' in a node_modules folder beside 'mynodejsfile.js'. 这会将依赖项“ mypackage”安装在“ mynodejsfile.js”旁边的node_modules文件夹中。

Then you will be able to 然后,您将能够

require('mypackage')

in the 'mynodejsfile.js'. 在“ 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. 如果要创建HTML内容,则节点将直接在浏览器中使用节点文件,该节点文件将与nodejs解释器一起使用。

When you use html file with script you use a client side code, which will not be understood by your browser. 当您将HTML文件与脚本一起使用时,会使用客户端代码,您的浏览器将无法理解该代码。 When writing client code (for browser) you will use browser ECMA 5/6 code, and not nodejs code. 在编写客户端代码(用于浏览器)时,您将使用浏览器ECMA 5/6代码,而不是nodejs代码。

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. 在我看来,您正在混淆客户端javascript功能的行为(例如将脚本文件包含到网页中)并构建NodeJS服务器。 As Matthi pointed out, Node can interpret way more than the browser. 正如Matthi所指出的,Node可以比浏览器更多地解释方式。

Regarding the necessity to constantly refresh your server instance I can hardly recommend nodemon to you. 关于不断刷新服务器实例的必要性,我很难向您推荐nodemon By default nodemon restarts your server after each code change, removing the problem of manually restarting the server in the development stage. 默认情况下,nodemon在每次代码更改后都会重新启动服务器,从而消除了在开发阶段手动重新启动服务器的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM