在 Windows 或 Linux 中,我想要一种将 args 传递给 a 的方法npm script,但将它们作为环境变量注入
从命令行,我会以这种方式启动我的项目:
npm run start -- --env=dev --host=localhost --port=1234
Run Code Online (Sandbox Code Playgroud)
为了使用我的 cli 参数并将它们作为环境变量注入(无论平台如何),我使用了cross-env npm 包:
包.json
"scripts": {
"start": "cross-env env=%env% host=%host% port=%port% my-app"
},
Run Code Online (Sandbox Code Playgroud)
我知道上面的语法无效,但是该start脚本是否可以以某种方式消耗我传递的参数而不是将它们转发到my-app?
我正在使用这样的东西:
\n\nschema.number\n .lessThan(\n value,\n `You own only ${value}.`)\nRun Code Online (Sandbox Code Playgroud)\n\n效果很好。但是,如果 value = 60,则所有 60 以内的数字都可以工作,但没有 60。我不能这样做:value + 1。有没有类似的方法:lessThanOrEqualto()?\n我需要 (\xe2\x88\ x9e; 60>\n我有 (\xe2\x88\x9e; 60)
\n\n\n在 Windows 10 和 npm 版本 6.9.0 下,我无法让以下脚本运行:
"build:css": "postcss --use autoprefixer -b 'last 2 versions' < ./static/css/main.css"
Run Code Online (Sandbox Code Playgroud)
我总是在 Windows 控制台中收到相同的错误,如下所示:
Plugin Error: Cannot find module 'autoprefixer'
我尝试将语法更改为以下语法,但没有结果:
"build:css": "postcss --use autoprefixer -b \"last 10 versions\" ./static/css/main.css -o ./static/css/main-prefixed.css"
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这里有什么问题吗?老实说,我对这个 npm 脚本很陌生。
我的package.json看起来像这样:
{
"name": "test-automate-npm",
"version": "1.0.0",
"description": "test",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"scss": "echo 'Compiling SCSS' && node-sass --watch scss -o ./static/css",
"build": "npm run scss && …Run Code Online (Sandbox Code Playgroud) 在准备自动脚本并安装一些软件包来自动执行任务后,我收到以下错误:
npm ERR! file C:\Users\windw\Desktop\bootstrap4\package.json
npm ERR! code EJSONPARSE
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected string in JSON at position 455 while parsing '{
npm ERR! JSON.parse "name": "confusion",
npm ERR! JSON.parse "version": '
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
Run Code Online (Sandbox Code Playgroud)
下面是我的package.json文件
{
"name": "confusion",
"version": "1.0.0",
"description": "This is a website for Ristorante Con Fusion",
"main": "index.html",
"scripts": {
"start": …Run Code Online (Sandbox Code Playgroud) 我正在构建一个 React 16.13.0 应用程序。我想根据环境以不同的方式配置端点,因此我在组件 src/containers/FormContainer.jsx 中进行了设置,...
class FormContainer extends Component {
static DEFAULT_COUNTRY = 484
static REACT_APP_PROXY = process.env.REACT_APP_PROXY
...
Run Code Online (Sandbox Code Playgroud)
我想在本地构建我的生产项目。然而我在本地定义了这个变量
localhost:client davea$ echo $REACT_APP_PROXY
http://localhost:9090
Run Code Online (Sandbox Code Playgroud)
在我运行“npm run-script build”之后,我注意到它被编译到我的构建文件中......
(function(e){return e.json()})).then((function(t){console.log(t),n=t.map((function(e){return e})),e.setState({provinces:n})}))}}]),t}(n.Component);S.DEFAULT_COUNTRY=484,S.REACT_APP_PROXY="http://localhost:9090"
Run Code Online (Sandbox Code Playgroud)
有没有办法不让 React 自动解析环境变量,而是从生产环境中获取它?也许我需要调整我的构建脚本?以下是我的 package.json 文件中定义的内容...
localhost:client davea$ cat package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"bootstrap": "^4.4.1",
"jquery": "^1.9.1",
"react": "^16.12.0",
"react-bootstrap": "^1.0.0-beta.17",
"react-dom": "^16.12.0",
"react-native-flash-message": "^0.1.15",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.1",
"typescript": "^3.8.3"
},
"scripts": {
"start": "react-scripts start", …Run Code Online (Sandbox Code Playgroud) 我想执行 npm 中脚本可以执行的内容。
例如,我安装了 vue-press 并且我想 exec vuepress eject。在我的脚本中,我可以看到vuepress可以访问,但我没有以下脚本eject:
"scripts": {
"dev": "vuepress dev docs",
"build": "vuepress build docs"
},
"license": "MIT",
"devDependencies": {
"vuepress": "^1.3.1",
"@vuepress/plugin-back-to-top": "^1.3.1",
"@vuepress/plugin-medium-zoom": "^1.3.1"
}
Run Code Online (Sandbox Code Playgroud)
所以我可以创建一个脚本供一种用途或使用任何命令npm exec。我正在寻找后一种解决方案。
有什么想法或者如何在不重新开始的情况下调试它吗?在 API、GraphQL 和 CI 上投入了大量时间。
\n这是一个普通的 create-react-app,在 App.js 中进行了一些细微的更改。使用节点 12.18.3 和 npm 6.14.8 在本地启动和构建。使用 npm@6.9.0 和 node@v10.16.0 进行 Amplify 构建失败。
\n我试图避免使用自定义 Docker 映像来升级节点和 npm。
\n编辑以添加 index.js - 构建日志来自尝试将 .js 附加到 ./App 导入
\nimport ReactDOM from 'react-dom';\nimport './index.css';\nimport App from './App';\nimport * as serviceWorker from './serviceWorker';\n\nReactDOM.render(\n <React.StrictMode> \n <App />\n </React.StrictMode>,\n document.getElementById('root')\n);\n\n// If you want your app to work offline and load faster, you can change\n// unregister() to register() below. Note this comes with some …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种在使用时运行脚本的方法npm run <script-name>。
我当前的用例是我想运行一个脚本,以确保在运行任何其他脚本之前正确设置开发环境。
所以,我需要的是这样的:
{
"run-before-all-scripts": "./setup_environment.sh",
"scripts": {
"script_one": "do something",
"script_two": "do somethingelse"
}
...
}
Run Code Online (Sandbox Code Playgroud)
我想出的最好的办法是:
{
"scripts": {
"setup_environment": "./setup_environment.sh",
"script_one": "npm run setup_environment && do something",
"script_two": "npm run setup_environment && do somethingelse"
}
...
}
Run Code Online (Sandbox Code Playgroud)
我想出的唯一更好的解决方案是围绕命令创建一个包装器 shell 脚本npm,这样我就可以调用fancy_npm_run <command>. 但这看起来有点混乱。
有更好的方法来处理这个问题吗?
我已经阅读了多个脚本上的 npm script hook,但它并不是在所有脚本之前运行,也没有得到答复。
我有一个像这样的 package.json :
...
"scripts": {
"dev": "webpack --config webpack.dev.config.js --mode development --progress --colors",
"postdev": "if (Test-Path \"./postdev.sh\" ) { echo \"file exists\"; ./postdev.sh }"
},
...
Run Code Online (Sandbox Code Playgroud)
如何检查文件“postdev.sh”是否存在,然后在 NPM 脚本部分启动它?我在终端中运行该命令并且运行正常,但如果我尝试启动该 npm 脚本,它会显示“意外出现:“./postdev.sh”。”
我为每个操作系统都有这 2 个 npm:
"pack": "cd dist/fold1 && type script11.js script22.js script33.js > all.js",
"pack-unix": "cd dist/fold1 && cat script11.js script22.js script33.js > all.js"
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它们成为一个命令?
喜欢:
"pack": "cd dist/fold1 && <if win than type else cat> script11.js script22.js script33.js > all.js",
npm-scripts ×10
npm ×9
node.js ×4
reactjs ×3
javascript ×2
autoprefixer ×1
aws-amplify ×1
formik ×1
json ×1
postcss ×1
webpack ×1
windows ×1
yup ×1