不能运行“ npm start”

时间:2020-06-10 21:59:18

标签: javascript node.js npm

这是我的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

3 个答案:

答案 0 :(得分:1)

您需要在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的工作原理。

答案 1 :(得分:1)

您需要在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是主项目目录。在martina中,可以找到index.jspackage.json

答案 2 :(得分:-1)

{
  "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"
}

通过查看文件,您的软件包出现。您刚刚创建了项目。您的index.js文件目录似乎不正确。您可以在package.json目录本身中创建index.js文件,然后运行节点index.js或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

尝试这个。您的解决方案将完成。如果您仍然遇到错误,请告诉我