如何将自定义脚本添加到运行 javascript 文件的 package.json 文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36433461/
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 19:04:09  来源:igfitidea点击:

How do I add a custom script to my package.json file that runs a javascript file?

javascriptnode.jspackage.jsonrun-script

提问by Jake.JS

I want to be able to execute the command script1in a project directory that will run node script1.js.

我希望能够script1在将运行的项目目录中执行命令node script1.js

script1.jsis a file in the same directory. The command needs to be specific to the project directory, meaning that if I send someone else the project folder, they will be able to run the same command.

script1.js是同目录下的文件。该命令需要特定于项目目录,这意味着如果我将项目文件夹发送给其他人,他们将能够运行相同的命令。

So far I've tried adding:

到目前为止,我已经尝试添加:

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

to my package.json file but when I try running script1I get the following output:

到我的 package.json 文件,但是当我尝试运行时,script1我得到以下输出:

zsh: command not found: script1

Does anyone know the steps necessary to add the script mentioned above to the project folder?

有谁知道将上述脚本添加到项目文件夹所需的步骤?

*Note: the command can not be added to the bash profile (cannot be a machine specific command)

*注意:该命令不能添加到 bash 配置文件中(不能是特定于机器的命令)

Please let me know if you need any clarification.

如果您需要任何说明,请告诉我。

回答by wesleysmyth

Custom Scripts

自定义脚本

npm run-script <custom_script_name>

npm run-script <custom_script_name>

or

或者

npm run <custom_script_name>

npm run <custom_script_name>

In your example, you would want to run npm run-script script1or npm run script1.

在您的示例中,您希望运行npm run-script script1npm run script1

See https://docs.npmjs.com/cli/run-script

请参阅https://docs.npmjs.com/cli/run-script

Lifecycle Scripts

生命周期脚本

Node also allows you to run custom scripts for certain lifecycle events, like after npm installis run. These can be found here.

Node 还允许您为某些生命周期事件运行自定义脚本,例如npm install运行后。这些可以在这里找到。

For example:

例如:

"scripts": {
    "postinstall": "electron-rebuild",
},

This would run electron-rebuildafter a npm installcommand.

这将electron-rebuildnpm install命令后运行。

回答by Sujeet Jaiswal

I have created the following, and it's working on my system. Please try this:

我创建了以下内容,它在我的系统上运行。请试试这个:

package.json:

包.json:

{
  "name": "test app",
  "version": "1.0.0",
  "scripts": {
    "start": "node script1.js"   
  }
}

script1.js:

脚本1.js:

console.log('testing')

From your command line run the following command:

从命令行运行以下命令:

npm start

Additional use case

附加用例

My package.json file has generally the following scripts, which enable me to watch my files for typescript, sass compilations and running a server as well.

我的 package.json 文件通常包含以下脚本,这使我能够查看我的文件以进行打字稿、sass 编译和运行服务器。

 "scripts": {
    "start": "concurrently \"sass --watch ./style/sass:./style/css\" \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w", 
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 
  }

回答by Jake.JS

Steps are below:

步骤如下:

  1. In package.json add:

    "bin":{
        "script1": "bin/script1.js" 
    }
    
  2. Create a binfolder in the project directory and add file runScript1.jswith the code:

    #! /usr/bin/env node
    var shell = require("shelljs");
    shell.exec("node step1script.js");
    
  3. Run npm install shelljsin terminal

  4. Run npm linkin terminal

  5. From terminal you can now run script1which will run node script1.js

  1. 在 package.json 添加:

    "bin":{
        "script1": "bin/script1.js" 
    }
    
  2. bin在项目目录中创建一个文件夹并添加runScript1.js带有代码的文件:

    #! /usr/bin/env node
    var shell = require("shelljs");
    shell.exec("node step1script.js");
    
  3. npm install shelljs在终端运行

  4. npm link在终端运行

  5. 从终端您现在可以运行script1将运行node script1.js

Reference: http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm

参考:http: //blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm

回答by FindOutIslamNow

Example:

例子:

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "build_c": "ng build --prod && del \"../../server/front-end/*.*\" /s /q & xcopy /s dist \"../../server/front-end\"",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

As you can see, the script "build_c" is building the angular application, then deletes all old files from a directory, then finally copies the result build files.

如您所见,脚本“build_c”正在构建 angular 应用程序,然后从目录中删除所有旧文件,最后复制结果构建文件。

回答by Soban Arshad

Lets say in scripts you want to run 2 commands with a single command:

让我们在脚本中说你想用一个命令运行 2 个命令:

"scripts":{
  "start":"any command",
  "singleCommandToRunTwoCommand":"some command here && npm start"
}

Now go to your terminal and run there npm run singleCommandToRunTwoCommand.

现在转到您的终端并在那里运行npm run singleCommandToRunTwoCommand