简体   繁体   English

intellij想法设置空白的node.js项目

[英]intellij idea setting up blank node.js project

I'm kinda new to intellij idea. 我对intellij的想法有点陌生。

The node.js project template with intellij IDEA is a node.js express project — which is nice (you get web server hooked up to run your project with, etc), but with all sorts of ugly stuff in it like jade . 带有intellij IDEA的node.js项目模板是一个node.js express项目-很好(您可以连接Web服务器以运行您的项目,等等),但其中包含各种丑陋的东西,例如jade I just want a no-frills, no nonsense node.js project... even http is optional. 我只想要简洁,没有胡说的node.js项目...甚至http是可选的。

I could see wanting a node.js project with basic web handling stuff like: body-parser, cookie-parser, serve-favicon, method-override, and morgan (which does http logging) — but that is really a bonus question/question for another day. 我可以看到想要一个具有基本Web处理功能的node.js项目,例如:body-parser,cookie-parser,serve-favicon,method-override和morgan(可进行http记录),但这确实是一个额外的问题/问题再换一天 Right now I just want a project template where I can press run afterwards and see the output on a console or something. 现在,我只需要一个项目模板,然后可以在其上按运行,然后在控制台或其他内容上查看输出。

From my reckoning you currently have 2 choices: 据我估计,您目前有2个选择:

1: Create an empty project and run the app using a Node.js interpreter. 1:创建一个空项目,然后使用Node.js解释器运行该应用程序。
This would mean going through the New Project wizard and selecting the Empty Project option. 这意味着要通过“ 新建项目”向导并选择“ 清空项目”选项。 Then start developing your Node.js application. 然后开始开发Node.js应用程序。

When it comes to running the application, you'll need to define a new Run/Debug Configuration or edit one you might currently have. 在运行应用程序时,您需要定义一个新的Run/Debug Configuration或编辑当前可能拥有的配置。
When doing this, click the green + button, select Node.js . 执行此操作时,单击绿色+按钮,选择Node.js
From there you can name your configuration, ensure "Activate Tool Window" is checked and configure which JavaScript file is your main file (eg app.js ). 从那里您可以命名您的配置,确保选中“激活工具窗口”,并配置哪个JavaScript文件是您的主文件(例如app.js )。

This method can be a bit fiddly (especially at the beginning with folder/package creation) as you're not using a Node.js module so IntelliJ doesn't really know that you're trying to create a Node.js application. 由于您没有使用Node.js模块,因此此方法可能有点儿怪异(尤其是在创建文件夹/包的开始),因此IntelliJ并不真正知道您正在尝试创建Node.js应用程序。


2: Create an Node.js Express App, Clear it out and Save it as a Project Template 2:创建一个Node.js Express应用程序,将其清除并将其另存为项目模板
First of all, go through the New Project wizard and create a Node.js Express App . 首先,通过New Project向导创建一个Node.js Express App Once your IDE has finished indexing and installing any Node.js packages it needs: Start deleting folders! IDE完成索引并安装所有Node.js软件包后,需要:开始删除文件夹!
Node.js is designed to be used in a modular way so you can pretty much remove everything. Node.js设计为以模块化方式使用,因此您几乎可以删除所有内容。 I recommend deleting everything except for: 我建议删除以下所有内容:

.idea/ because this is an IntelliJ thing and is needed. .idea/因为这是IntelliJ事情,是必需的。
node_modules/ because IntelliJ configures this as your library root so all future modules you use will be imported into this automatically. node_modules/因为IntelliJ将其配置为您的库根目录,因此您将来使用的所有模块将自动导入此库中。 You can delete everything inside the node_modules/ directory though. 但是,您可以删除node_modules/目录中的所有内容。
app.js because most Node.js application have a main JS file which is called app.js so you might as well keep this one. app.js因为大多数Node.js应用程序都有一个主JS文件,称为app.js因此您最好保留该文件。 Still delete everything inside but keep the file. 仍然删除其中的所有内容,但保留文件。
<ProjectName>.iml because this is an IntelliJ thing and is needed. <ProjectName>.iml因为这是IntelliJ事情,是必需的。
package.json because this is your dependency management file. package.json因为这是您的依赖项管理文件。 You can delete everything inside of the scripts and dependencies JSON objects. 您可以删除scriptsdependencies JSON对象中的所有内容。

Now if you're doing this in IntelliJ (ie NOT WebStorm) you have the option to save this as a Project Template . 现在,如果您正在IntelliJ中进行此操作(即,不是WebStorm),则可以选择将其另存为项目模板
This essentially means that when you come to developing your next Node.js application, instead of doing the above all over again, you can just select this custom template and get developing!. 这实质上意味着,当您要开发下一个Node.js应用程序时,不必重新进行上述操作,您只需选择此自定义模板即可开始开发!
To save the project as a Project Template , go to: 要将项目另存为项目模板 ,请转到:

Tools > Save Project as Template...

Then fill in the boxes with whatever you want need/want and click OK . 然后在方框中填写您需要/想要的内容,然后点击确定

If you're running WebStorm, you'll have to save the project somewhere (easily accessible folder, Github, etc.) and then make a clone/fork it when you want to develop a new application. 如果您正在运行WebStorm,则必须将项目保存在某个地方(易于访问的文件夹,Github等),然后在要开发新应用程序时对其进行克隆/复制。


One of the important things to remember is that Node.js is still a pretty new language. 要记住的重要事情之一是Node.js仍然是一种相当新的语言。

Express Apps are very common in the Node.js development world and so creating a Template/Framework for them is a simple, good business move for Jetbrains. Express Apps在Node.js开发世界中非常普遍,因此为它们创建模板/框架对于Jetbrains而言是一项简单而有效的商业举措。

I'm sure (and on forums Jetbrains has indicated) that Jetbrains will continue to make more Node.js templates/frameworks supported on their IDE(s) but I suspect most will be added into WebStorm as that is their main IDE for JavaScript development. 我敢肯定(在Jetbrains上已在论坛上表示),Jetbrains将继续在其IDE上制作更多支持的Node.js模板/框架,但我怀疑大多数将添加到WebStorm中,因为这是其JavaScript开发的主要IDE 。

What you see by default ("body-parser", "cookie-parser", "debug", "express", "jade", "morgan", "serve-favicon") are all npm dependencies, so you can remove them using npm as well. 默认情况下,您看到的内容(“ body-parser”,“ cookie-parser”,“ debug”,“ express”,“ jade”,“ morgan”,“ serve-favicon”)都是npm依赖项,因此您可以删除它们也使用npm。

Complementing @Harmelodic answer, first remove these dependencies one by one: 作为@Harmelodic答案的补充,首先一个一个地删除这些依赖项:

Assuming you already have npm installed in your system (comes with NodeJS), go to your project root folder: 假设您已经在系统中安装了npm(NodeJS附带),请转到项目根文件夹:

$ cd my/project/folder

Then use npm uninstall : 然后使用npm uninstall

$npm uninstall body-parser
$npm uninstall cookie-parser
$npm uninstall debug
... etc.

After removing all the dependencies, you will see the "dependencies" JSON object of the project.json file empty: 删除所有依赖项后,您将看到project.json文件的"dependencies" JSON对象为空:

"dependencies": {}

Also, the node_modules folder will be emty, and the package-lock.json file will be just a skeleton. 此外, node_modules文件夹将为emty,而package-lock.json文件将仅为框架。

After this create your project template. 之后,创建您的项目模板。

You can use Express Application Generator . 您可以使用Express Application Generator You can generate app skeleton quickly and you have many option to select template engine like jade, ejs, hbs & more. 您可以快速生成应用程序骨架,并且有很多选项来选择模板引擎,例如jade,ejs,hbs等。

npm install express-generator -g

After that, express [AppName] and open into Intellij. 之后,表达[AppName]并打开Intellij。

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

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