Javascript 如何使用 npm 脚本运行 js 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32964900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:26:34  来源:igfitidea点击:

How do you run a js file using npm scripts?

javascriptnode.jsnpm

提问by Richard

I can't get npm to work. My package.json file has

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

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

and I have a build.js file in the same folder that just console.logs.

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

When I run

当我跑

npm run build

I get the error

我收到错误

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

and if I move the build.js file and change my package.json file to have a subfolder

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

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

then I get the error

然后我得到错误

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

What's going wrong? I'm copying the example documentation.

怎么了?我正在复制示例文档

回答by Luca Filosofi

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

npm run buildOR npm run-script build

npm run build或者 npm run-script build



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

npm start

npm start



NB:you were missing the { brackets }and the nodecommand

注意:你错过了{ brackets }节点命令

folder structure is fine:

文件夹结构很好:

+ build
  - package.json
  - build.js

回答by Shanoor

You should use npm run-script buildor npm build <project_folder>. More info here: https://docs.npmjs.com/cli/build.

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