node.js中的'npm start'不起作用

时间:2019-08-03 14:14:04

标签: node.js

节点App.js有效,但是npm start不起作用。

我刚刚关注了this nodejs tutorial(教程由朝鲜语编写)。

我试图将代码更改为:

“脚本”:{     “开始”:“ webpack-dev-server”  } 但这没用。

这是我的代码: App.js

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

package.json

{
  "name": "codlab-nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node js"
  },
  "author": "",
  "license": "ISC"
}

package-lock.json

{
  "name": "codlab-nodejs",
  "version": "1.0.0",
  "lockfileVersion": 1
}

这是错误的消息。

C:\Users\김동희\codlab-nodejs>npm start

> codlab-nodejs@1.0.0 start C:\Users\김동희\codlab-nodejs
> node js

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

Error: Cannot find module 'C:\Users\김동희\codlab-nodejs\js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! codlab-nodejs@1.0.0 start: `node js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the codlab-nodejs@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!     C:\Users\김동희\AppData\Roaming\npm-cache\_logs\2019-08-03T14_03_42_211Z-debug.log

2 个答案:

答案 0 :(得分:4)

在package.json中,更改属性“开始”的值。

替换:

body {
  margin: 0;
}
.sidebar-toggle {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  padding: 10px;
  background: red;
  color: white;
}
#sidebar-toggle-input {
  display: none;
}
#sidebar-toggle-input+label:before {
  content: "➡️";
}
#sidebar-toggle-input:checked + label:before {
  content: "❌"
}
.sidebar {
  position: fixed;
  width: 200px;
  transition: 0.3s;
  background: blue;
  height: 100vh;
  top: 0;
  left: 0;
  transform: translateX(-100%);
}
#sidebar-toggle-input:checked ~ .sidebar {
  transform: none;
}

使用:

<input type="checkbox" id="sidebar-toggle-input" />
<label class="sidebar-toggle" for="sidebar-toggle-input"></label>
<div class="sidebar"></div>

答案 1 :(得分:2)

start npm脚本试图运行名为js的文件,该文件不存在,因此失败。

相反,将start npm脚本设置为要求Node.js运行App.js文件的正确命令。

您的package.json将包含以下内容:

  "scripts": {
    "start": "node App.js"
  }