【问题标题】:Error when running "npm start"?运行“npm start”时出错?
【发布时间】:2016-12-11 18:51:50
【问题描述】:

我今天开始使用电子,并按照指南创建了一个简单的应用程序浏览器窗口,我在命令窗口中运行 npm start,输入后它显示了很多错误,我已经发布了以下信息和我的代码。

控制台错误:

C:\Users\Administrator\Desktop\electron>npm start

> breef@1.0.0 start C:\Users\Administrator\Desktop\electron
> node index.js

module.js:471
    throw err;
    ^

Error: Cannot find module 'app'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\electron\index.js:1:73)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.9.2
npm ERR! npm  v3.10.9
npm ERR! code ELIFECYCLE
npm ERR! breef@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the breef@1.0.0 start script 'node index.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the breef package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node index.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs breef
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls breef
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\Administrator\Desktop\electron\npm-debug.log

这是我的 package.json:

{
  "name": "breef",
  "version": "1.0.0",
  "description": "A simple chat application.",
  "main": "index.js",
  "dependencies": {
    "electron": "^1.4.12"
  },
  "devDependencies": {},
  "scripts": {
    "test": "breeftestcmd",
    "start": "node index.js"
  },
  "keywords": [
    "breef"
  ],
  "author": "Sage & Sh4wn",
  "license": "ISC"
}

这是我的 index.js:

var app = require("app")
var browserWindow = require("browser-window")

app.on('ready', function() {
    var mainWindow = new browserWindow({
        width: 800,
        height: 600
    })

    mainWindow.loadUrl('http://google.com');
})

【问题讨论】:

  • appnode_modules 中的模块还是您项目中的另一个文件?要要求您的项目自己的模块,the path should start with /, ./, or ../ - 例如require("./app")。 "如果没有前导的 '/'、'./' 或 '../' 来表示文件,则模块必须是核心模块或从 node_modules 文件夹加载。"跨度>

标签: javascript node.js electron


【解决方案1】:

如果 app 是一个模块,则它在您的 package.json 中缺失。如果没有,您应该使用路径来要求它

var app = require('./app');

【讨论】:

    【解决方案2】:

    您遵循的指南已过时,从 Electron v1.0.0 开始,内置 Electron 模块的导入方式已更改。这是正确的方法:

    const {app, BrowserWindow} = require("electron")
    

    您还可能会发现阅读涵盖该主题的上一个问题会有所帮助:What's the proper way to require in Node.js?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 2022-11-19
      • 2021-01-08
      • 2020-07-02
      • 2020-12-18
      相关资源
      最近更新 更多