javascript 在哪里放置 JS 文件让 NodeJS 看到它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8359719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to place JS files for NodeJS to see them
提问by B?dlen
I've just installed NodeJS on my Mac, and i got it working in the terminal, using inline scripting like "console.log('Hello world'); works fine.
我刚刚在我的 Mac 上安装了 NodeJS,我让它在终端中工作,使用内联脚本,如“console.log('Hello world'); 工作正常。
But where do i place JS files for NodeJS to find them? Can i specify the root folder NodeJS to look for file in?
但是我应该把 NodeJS 的 JS 文件放在哪里才能找到它们呢?我可以指定根文件夹 NodeJS 来查找文件吗?
I followed this guide: http://nodeguide.com/beginner.html#learning-javascriptbut i cannot get any of the samlpe to work where i reference a script file.
我遵循了本指南:http: //nodeguide.com/beginner.html#learning-javascript 但我无法让任何 samlpe 在我引用脚本文件的地方工作。
回答by Alec Gorge
You put them in whatever folder you want. It is common practice to put each application in a different folder.
你把它们放在你想要的任何文件夹中。通常的做法是将每个应用程序放在不同的文件夹中。
Then you run node.js like this:
然后像这样运行 node.js:
node /path/to/file.js
Or like this:
或者像这样:
cd /path/to/
node file.js
Where file.js
might look something like this:
哪里file.js
可能看起来像这样:
console.log('hello world');
回答by Olemak
You'll have to navigate to the correct folder "manually", in the Node Command Line Interface (CLI).
您必须在节点命令行界面 (CLI) 中“手动”导航到正确的文件夹。
If you need to change drives, type the drive letter and a colon to switch to that drive, like so;
如果您需要更改驱动器,请键入驱动器号和冒号以切换到该驱动器,如下所示;
C:>(<- this is the line prompt, yeah? Just add this after it -> D:
C:>(<- 这是行提示,是吗?在它后面加上这个 -> D:
That changes the drive. Now write cd (CD = "Change Directory") and the name of the direcotry you want to go to the directory your stuff is in:
这改变了驱动器。现在写 cd (CD = "Change Directory") 和你想要进入你的东西所在目录的目录的名称:
D:>(<- the new prompt. Write something like this after it: ->) cd myprosject\subfoldernameD:\myproject\subfoldername>(<- your new line prompt - if "myproject\subfoldername" exists)
D:>(<- 新提示。在它后面写这样的东西:->) cd myprosject\subfoldername D:\myproject\subfoldername>(<- 你的新行提示 - 如果“myproject\subfoldername”存在)
now ask node to execute your script (that is stored in myproject\subfoldername, like so;
现在要求节点执行您的脚本(存储在 myproject\subfoldername 中,像这样;
D:\myproject\subfoldername> node helloworld.js
D:\myproject\subfoldername> 节点 helloworld.js
Remember to write "node" first - otherwise the command won't go to node, but to the OS, which will probably just open up the js file in a text editior instead of running the goodies inside.
记住先写“节点” - 否则命令不会转到节点,而是转到操作系统,它可能只会在文本编辑器中打开 js 文件,而不是在里面运行好东西。
回答by Supun Dharmarathne
It is very easy.. Go to your command line. navigate to the file location.. then simply run the node helloworld.
这很容易.. 转到您的命令行。导航到文件位置.. 然后只需运行节点 helloworld。
回答by Christopher
I'm not sure I understand. it doesnt 'look' anywhere for your .js files you point at them when you run node. Like so, on the command line:
我不确定我是否理解。当您运行 node.js 文件时,它不会在任何地方“查找”您指向它们的 .js 文件。像这样,在命令行上:
node mynodeapp.js
If you're meaning where does it look for your .js files as modules, when requirign them, like so:
如果你的意思是在哪里寻找你的 .js 文件作为模块,那么在重新定义它们时,就像这样:
var mymodule = require("mymodule");
Then it will look inside a folder names node_modules
. But I'm sure you're looking for my first example above.
然后它会在文件夹名称中查找node_modules
。但我相信你正在寻找我上面的第一个例子。