node.js 如何让 npm start 在不同的目录中?

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

How can I get npm start at a different directory?

node.js

提问by Blaise

I usually cdinto the app directory and then run npm start.

我通常cd进入应用程序目录,然后运行npm start.

It is my feeling that there ought to be some way to run npm startwith a path parameter. But, the npm start documentationcontains no such feature.

我的感觉是应该有某种方法可以npm start使用路径参数运行。但是,npm start 文档不包含此类功能。

I tried myself only to find npm start ./myappdoes not work. Is there a way to do that?

我试过自己才发现npm start ./myapp不起作用。有没有办法做到这一点?

回答by Amaury Liet

This one-liner should work:

这个单行应该工作:

npm start --prefix path/to/your/app

Corresponding doc

对应文件

回答by itiic

Below Command where projectis a folder which contains package.jsonfile

在命令下面project是一个包含package.json文件的文件夹

npm run --prefix project ${COMMAND}

is working as well. Useful in Docker based applications.

也在工作。在基于 Docker 的应用程序中很有用。

回答by Rafael17

npm start --prefix path/to/your/app

npm start --prefix path/to/your/app

& inside package.json add the following script

& 在 package.json 中添加以下脚本

"scripts": {
   "preinstall":"cd $(pwd)"
}

回答by Or Duan

I came here from google so it might be relevant to others: for yarnyou could use:

我从谷歌来到这里,所以它可能与其他人相关:因为yarn你可以使用:

yarn --cwd /path/to/your/app run start 

回答by babca

This one-liner should work too:

这个单行也应该工作:

(cd /path/to/your/app && npm start)

Note that the current directory will be changed to /path/to/your/app after executing this command. To preserve the working directory:

请注意,执行此命令后,当前目录将更改为 /path/to/your/app。要保留工作目录:

(cd /path/to/your/app && npm start && cd -)

I used this solution because a program configuration file I was editing back then didn't support specifying command line arguments.

我使用这个解决方案是因为我当时正在编辑的程序配置文件不支持指定命令行参数。

回答by zangw

Per this npm issue list, one work around could be done through npm config

根据此npm 问题列表,可以通过 npm config 完成一项解决方法

name: 'foo'
config: { path: "baz" },
scripts: { start: "node ./$npm_package_config_path" }

Under windows, the scriptscould be { start: "node ./%npm_package_config_path%" }

在窗户下,scripts可能是{ start: "node ./%npm_package_config_path%" }

Then run the command line as below

然后运行命令行如下

npm start --foo:path=myapp