【发布时间】:2020-02-02 16:13:00
【问题描述】:
我创建了一个全新的项目:
$ npx create-react-app test-react-app && cd test-react-app && npm start
这里是回购:https://github.com/tlg-265/test-react-app
我创建了一个新脚本:/prepare-project.js,内容如下:
let project = 'ferrari' // replace this value with the one passed via: $ npm start [what to put here?]
console.log(`########################`);
console.log(`###### The current project is: ${project} ######`);
console.log(`########################`);
文件内容:/package.json是:
{
"name": "test-react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"prestart": "node prepare-project.js"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
在哪里可以看到我配置了pre hook:
"prestart": "node prepare-project.js"
我的目标是跑步:
$ npm start [what to put here?]
所以在脚本中:/prepare-project.js 我可以读取该值。
我试过了:
$ node prepare-project.js --project=mclaren
尝试从脚本内部读取该值时没有运气:prepare-project.js。
知道如何实现这一目标吗?
谢谢!
【问题讨论】:
-
为什么要将该值放入 js 文件并传递给 npm 脚本。你为什么不把它放在一个环境中
.env文件 -
@ZohaibIjaz 因为该值不是静态的,并且会动态更改为任何随机值。
标签: javascript node.js reactjs package.json npx