【问题标题】:'npm start' in node.js doesn't worksnode.js 中的“npm start”不起作用
【发布时间】:2019-08-03 22:19:07
【问题描述】:

node App.js 有效,但 npm start 无效。

我刚刚关注this nodejs tutorial(教程是韩国人写的)。

我尝试将代码更改为:

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

这是我的代码: 应用.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

【问题讨论】:

    标签: node.js


    【解决方案1】:

    在您的 package.json 中更改属性“start”的值。

    替换:

    "start": "node js"
    

    与:

    "start": "node App.js"
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-10
        • 2023-03-06
        • 2017-07-24
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多