简体   繁体   English

Node.JS 在文件夹中找不到模块

[英]Node.JS can't find Module inside Folder

I made a folder.我做了一个文件夹。 Then cloned code from GH into it.然后将 GH 中的代码克隆到其中。 so it's a folder inside my folder.所以它是我文件夹中的一个文件夹。 I run (node index.js) and it throws 'error, module not found.我运行 (node index.js) 并抛出“错误,找不到模块”。 What can I do differently?我能做些什么不同的事情?

`josh@Joshs-MBP random % node index.js
node:internal/modules/cjs/loader:927
  throw err;
  ^

Error: Cannot find module '/Users/josh/Desktop/random/index.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:924:15)
    at Function.Module._load (node:internal/modules/cjs/loader:769:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}josh@Joshs-MBP random % `

Problem -问题 -

Running node index.js throws a module not found error.运行node index.js会引发模块未找到错误。

Solution -解决方案 -

You need to change directory one directory level deeper.您需要更深一层地更改目录。

You are currently in您目前在

/Users/josh/Desktop/random/index.js

You said you created a directory, then put a Github repo inside of that directory.您说您创建了一个目录,然后在该目录中放置了一个 Github 存储库。 That means you need to change directory into the Github repo still.这意味着您仍然需要将目录更改为 Github 存储库。

If you look at the error it shows that you are running node index.js inside of a directory on your Desktop.如果您查看错误,它表明您正在桌面上的目录中运行node index.js

Based on your description of putting a Github repo inside of a directory you created, you likely need to use cd to move into it.根据您将 Github 存储库放入您创建的目录中的描述,您可能需要使用cd才能进入该目录。

Solution -解决方案 -

  1. cd repoName

  2. ls to verify index.js is in the current location. ls来验证index.js是否在当前位置。

  3. When you see index.js in the output from ls type node index.js当您在ls的输出中看到index.js时,请输入node index.js

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

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