简体   繁体   中英

npm: run scripts from package.json from anywhere inside the project folder

I'm using the scripts section inside the package.json file to store some commands I have to run regularly.

 "scripts": {
    "test": "./test/phantomjs ./test/js/run_jasmine_test.coffee ./test/index.html",
    "rjs": "r.js -o ./js/app.build.js",
    "less": "lessc -x ./css/app.less > ./css/app.css"
  }

in every command I have got a ./ at the beginning of the path - this is why I can only call npm run-script rjs from the project's root directory. is there a way to reference the project's root directory inside the package.json so that I can run eg npm test from anywhere in my project?

I think it depends on the way you organize your project.

I created a sample project just to demonstrate. This is my sample project directory tree:

.
├── dir1
│   └── dir2
├── package.json
├── src
│   └── index.js
└── test
    └── test.js

NOTE: ./dir1/dir2 are just two empty directories for demonstration.

My package.json file looks like this:

{
  "name": "sample",
  "version": "1.0.0",
  "description": "sample",
  "main": "index.js",
  "scripts": {
    "test": "node test/test.js",
    "start": "node src/index.js"
  },
  "author": "yaniv",
  "license": "ISC"
}

Focus on the scripts section, those are two simple line, without considerations about my location.

If I change directory to /dir1/dir2 and execute npm test and the script I wrote on test property is executed properly without the use of ./ (which defines the current directory...)

Bottom line - I suppose if you re-organize your project and remove ./ will do the job in your case.

INIT_CWD

We can reference the root directory of the project throught the environment variable INIT_CWD that npm set for us!

From the doc :

Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run.

Example:

"tas:adminApp:build": "cd src/KidoService/AdminApp && npm run build && cd $INIT_CWD && gulp build_copyAdminApp",

And just because i love to think too much!

What if we didn't have such a variable set for us!

We can execute an env variable set command at all start! export ROOT_DIR=$PDW !

And then when we need it ! We can use it!

The above example will become

"tas:adminApp:build": "export ROOT_DIR=$PDW && cd src/KidoService/AdminApp && npm run build && cd $ROOT_DIR && gulp build_copyAdminApp",

You can see it's a nice technique ! We can use that to reference some directories that we want to go back to!

For windows we use set VAR=VALUE

considered creating a shell file that runs?

for example create a.sh file make it executable (chmod 777 file.sh)

and then cd to your nodejs project root, run the npm command, and then cd back into the directory you have just left?

adding the script to your environment will make it executable from anywhere...

alternatively will hard coding the full path be ok? (so rather than using./ - put /home/user/username/yourproject/phantomjs)

I'm using the scripts section inside the package.json file to store some commands I have to run regularly.

 "scripts": {
    "test": "./test/phantomjs ./test/js/run_jasmine_test.coffee ./test/index.html",
    "rjs": "r.js -o ./js/app.build.js",
    "less": "lessc -x ./css/app.less > ./css/app.css"
  }

in every command I have got a ./ at the beginning of the path - this is why I can only call npm run-script rjs from the project's root directory. is there a way to reference the project's root directory inside the package.json so that I can run eg npm test from anywhere in my project?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM