簡體   English   中英

無法運行“npm start”

[英]can't run "npm start"

這是我的 package.json 文件:

{
  "name": "martina",
  "version": "1.0.0",
  "description": "d",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "d"
  },
  "author": "",
  "license": "ISC"
}

我應該怎么辦? 順便說一下,我有一台 Mac!

編輯:我做了你們推薦的並添加了腳本啟動,但是現在當我運行“npm start”時,我得到了這個:

> martina@1.0.0 start /Users/martina
> node index.js

internal/modules/cjs/loader.js:589
    throw err;
    ^

Error: Cannot find module '/Users/martina/index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:587:15)
    at Function.Module._load (internal/modules/cjs/loader.js:513:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
    at startup (internal/bootstrap/node.js:303:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:872:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! martina@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the martina@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/martina/.npm/_logs/2020-06-11T08_05_28_654Z-debug.log

您需要在 scripts 下添加start scripts ,即:

{
  "name": "martina",
  "version": "1.0.0",
  "description": "d",
  "main": "index.js",
  "scripts": {
    "start": "echo STARTING...",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "d"
  },
  "author": "",
  "license": "ISC"
}

嘗試npm run test以了解scripts字段如何為package.json工作。

您需要在 scripts 下添加一個start scripts ,並在本地節點服務器上托管或啟動應用程序,添加命令node index.js作為start腳本的值。

{
  "name": "martina",
  "version": "1.0.0",
  "description": "d",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "d"
  },
  "author": "",
  "license": "ISC"
}

PS:運行命令npm start ,請確保您位於正確的項目目錄中。 文件index.jspackage.json需要作為該項目目錄的直接子項存在。

項目結構:

martina
  |- index.js
  |- package.json
  |- file1
  |- file2
  |- ...other files
  ...

在上面的項目結構中看到, martina是項目的主目錄。 package.json里面,可以找到index.jsmartina

{
  "name": "martina",
  "version": "1.0.0",
  "description": "d",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "d"
  },
  "author": "",
  "license": "ISC"
}

您的 package 通過查看文件出現。 您剛剛創建了項目。 看來你的 index.js 文件的目錄不正確。 您在 package.json 目錄本身中創建 index.js 文件,然后運行節點 index.js 或 npm 啟動命令

> martina@1.0.0 start /Users/martina
> node index.js

internal/modules/cjs/loader.js:589
    throw err;
    ^

Error: Cannot find module '/Users/martina/index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:587:15)
    at Function.Module._load (internal/modules/cjs/loader.js:513:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
    at startup (internal/bootstrap/node.js:303:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:872:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! martina@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the martina@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/martina/.npm/_logs/2020-06-11T08_05_28_654Z-debug.log

試試這個。 您的解決方案將完成。 如果您仍然遇到錯誤,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM