简体   繁体   English

找不到模块 node.js

[英]Cannot find module node.js

I created a new folder on my desktop and in that folder I put a file named app.js containing the following code.我在桌面上创建了一个新文件夹,并在该文件夹中放置了一个名为 app.js 的文件,其中包含以下代码。

function sayHello(name) {
    console.log("Hello" + name);
}

sayHello("Noah")

In the terminal I entered node app.js and was returned the following.在终端中,我输入了 node app.js 并返回了以下内容。

module.js:549
throw err;
^

Error: Cannot find module '/home/noah/app.js'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3

I followed a Digital Ocean tutorial to get everything installed and everything worked as described.我按照 Digital Ocean 教程安装了所有东西,并且一切都如描述的那样工作。 Am I putting my files in the wrong location?我是否将文件放在错误的位置?

try this in your terminal在你的终端试试这个

$ cd /home/noah
$ node app.js

or second option或第二个选择

$ node /home/noah/app.js

I prefer the first option.我更喜欢第一个选项。 Good luck.祝你好运。

Open the folder that contains your app.js file and click right mouse button + SHIFT.打开包含 app.js 文件的文件夹,然后单击鼠标右键 + SHIFT。 Then select 'Open command window here' inorder to open your terminal.然后选择“在此处打开命令窗口”以打开您的终端。

In the terminal check your node and npm versions to make sure both are installed correctly in your computer在终端中检查您的 node 和 npm 版本以确保它们都正确安装在您的计算机中

    $ node -v
    $ npm -v

Then然后

    $ npm install

to download and install necessary node modules to your app.将必要的节点模块下载并安装到您的应用程序中。

Finally try,最后试试,

    $ node app 

or或者

    $ node app.js

I was in the wrong directory.我在错误的目录中。 Using the desktop on Ubuntu was my problem, I will need to figure out how to store and create my files in the user directory to save myself from using cd frequently.在 Ubuntu 上使用桌面是我的问题,我需要弄清楚如何在用户目录中存储和创建我的文件,以避免经常使用 cd。

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

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