bdmason bdmason - 3 months ago 5x
Node.js Question

Is it possible to run scripts defined in package.json from outside of package.json's directory?

I'd like to replicate webpack's ability to be run from any directory with npm scripts.

For example this works with Webpack

webpack --config /absolute/path/to/webpack.config.js

Is there an equivalent for npm scripts, e.g.

/absolute/path/to/package.json/npm run build

I'm trying to avoid having to
in, run the script, then
out again.


There does not seem to be any option in npm to specify a folder, so I think you will have to cd into the directory, run your npm command, then cd out again (note that $OLDPWD automatically holds the previous working directory - you don't have to assign it):

cd /path/to/package.json; npm run build; cd $OLDPWD

However, if an error occurs during your npm call, you will be stuck in that directory.