【问题标题】:How do you run a js file using npm scripts?如何使用 npm 脚本运行 js 文件?
【发布时间】:2016-01-03 01:43:18
【问题描述】:

我无法让 npm 工作。我的 package.json 文件有

"scripts": { "build": "build.js" }

我在与 console.logs 相同的文件夹中有一个 build.js 文件。

当我跑步时

npm run build

我得到了错误

The system cannot execute the specified program.

npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v4.1.1
npm ERR! npm  v3.3.5
npm ERR! code ELIFECYCLE

如果我移动 build.js 文件并将我的 package.json 文件更改为有一个子文件夹

"scripts": { "build": "build/build.js" }

然后我得到错误

'build' is not recognized as an internal or external command, operable program or batch file.

怎么了?我正在复制example documentation

【问题讨论】:

    标签: javascript node.js npm


    【解决方案1】:
    { "scripts" :
      { "build": "node build.js"}
    }
    

    npm run buildnpm run-script build


    {
      "name": "build",
      "version": "1.0.0",
      "scripts": {
        "start": "node build.js"
      }
    }
    

    npm start


    注意:您缺少{ brackets }node 命令

    文件夹结构很好:

    + build
      - package.json
      - build.js
    

    【讨论】:

    • 这行得通。你能解释一下为什么需要“节点”吗?事后看来这是有道理的,但它不在文档中。
    • 需要节点,因为它需要启动您的脚本。你可以在这里输入任何命令。
    • 他不是“错过了node 命令”,他链接的文档也没有。
    【解决方案2】:

    您应该使用npm run-script buildnpm build <project_folder>。更多信息在这里:https://docs.npmjs.com/cli/build

    【讨论】:

    • 这不起作用 - 同样的错误。文档说 run-script 只是 run 的别名。
    【解决方案3】:

    打开项目文件夹并运行流程

    { "scripts" :
      { "build": "node build.js"}
    }
    

    npm run build 或 npm run-script build

    {
      "name": "build",
      "version": "1.0.0",
      "scripts": {
        "start": "node build.js"
      }
    }
    

    npm 开始

    【讨论】:

    • 你只是 c&p 6 年前 Luca Filosofi 的答案
    猜你喜欢
    • 2018-07-25
    • 2017-04-24
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多