如何向包中添加自定义脚本。运行javascript文件的json文件?

时间:2021-06-24 07:25:07

I want to be able to execute the command script1 in a project directory that will run node script1.js. script1.js is 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.

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

So far I've tried adding:

到目前为止,我试着补充说:

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

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

我的包。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.

如果你需要任何解释,请告诉我。

4 个解决方案

#1


88  

Custom Scripts

npm run-script <custom_script_name>

npm运行脚本< custom_script_name >

or

npm run <custom_script_name>

npm运行< custom_script_name >

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

在您的示例中,您可能希望运行npm run-script script1或npm 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 install is run. These can be found here.

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

For example:

例如:

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

This would run electron-rebuild after a npm install command.

这将在npm安装命令之后运行电子重建。

#2


8  

I have created the follwing, and its working on my system. Please try this

我已经创建了下面的东西,以及它对我的系统的作用。请试试这个

package.json:

package.json:

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

script1.js:

script1.js:

console.log('testing')

From your command line run the following command

从命令行运行以下命令

npm start

npm开始

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.

附加用例我的包。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" 
  }

#3


6  

Steps are below:

步骤如下:

  1. In package.json add:

    在包中。json添加:

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

    在项目目录中创建一个bin文件夹并添加文件runScript1。js代码:

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

    运行npm在终端安装shelljs。

  4. Run npm link in terminal

    在终端运行npm链接。

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

    现在可以从终端运行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

#4


0  

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”正在构建有棱角的应用程序,然后从目录中删除所有旧文件,最后复制结果构建文件。

#1


88  

Custom Scripts

npm run-script <custom_script_name>

npm运行脚本< custom_script_name >

or

npm run <custom_script_name>

npm运行< custom_script_name >

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

在您的示例中,您可能希望运行npm run-script script1或npm 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 install is run. These can be found here.

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

For example:

例如:

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

This would run electron-rebuild after a npm install command.

这将在npm安装命令之后运行电子重建。

#2


8  

I have created the follwing, and its working on my system. Please try this

我已经创建了下面的东西,以及它对我的系统的作用。请试试这个

package.json:

package.json:

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

script1.js:

script1.js:

console.log('testing')

From your command line run the following command

从命令行运行以下命令

npm start

npm开始

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.

附加用例我的包。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" 
  }

#3


6  

Steps are below:

步骤如下:

  1. In package.json add:

    在包中。json添加:

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

    在项目目录中创建一个bin文件夹并添加文件runScript1。js代码:

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

    运行npm在终端安装shelljs。

  4. Run npm link in terminal

    在终端运行npm链接。

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

    现在可以从终端运行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

#4


0  

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”正在构建有棱角的应用程序,然后从目录中删除所有旧文件,最后复制结果构建文件。