javascript - 将客户端和服务器 package.json 文件合并为一个

标签 javascript node.js reactjs package.json

我有一个由服务器端代码(在nodejs中)和客户端代码(使用react)组成的项目。我有单独的 package.json 文件,但我被要求将它们合并为一个。服务器文件位于项目根目录中,客户端与其余客户端代码位于/client 文件夹中。你能帮我做那件事吗?我可以将客户端文件复制到服务器客户端并进行一些修改吗?我找不到任何有用的东西。

package.json:

{
  "name": "curr-calc",
  "version": "1.0.0",
  "description": "currency calc using node.js and react.js",
  "main": "index.js",
  "repository": "",
  "author": "",
  "license": "MIT",
  "scripts": {
    "client": "cd client && yarn start",
    "server": "nodemon server.js",
    "dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\"",
    "lint": "eslint .",
    "babel": "babel --presets es2015 js/server.js -o build/main.bundle.js",
    "test": "yarn --cwd client run test",
    "heroku-postbuild": "cd client && yarn --production=false && yarn run build"
  },
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2",
    "express-session": "^1.15.6",
    "node-fetch": "^2.1.2",
    "open": "0.0.5",
    "path": "^0.12.7",
    "prop-types": "^15.6.1",
    "react-widgets": "^4.2.6",
    "redis": "^2.8.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.0.3",
    "babel-preset-env": "^1.6.1",
    "babel-preset-es2015": "^6.24.1",
    "concurrently": "^3.5.0",
    "eslint": "^4.19.1",
    "eslint-config-standard": "^10.2.1",
    "eslint-config-standard-react": "^5.0.0",
    "eslint-plugin-import": "^2.6.0",
    "eslint-plugin-jest": "^21.3.2",
    "eslint-plugin-node": "^5.1.0",
    "eslint-plugin-promise": "^3.5.0",
    "eslint-plugin-react": "^7.5.1",
    "eslint-plugin-standard": "^3.0.1",
    "fetch-mock": "^6.3.0",
    "node-fetch": "^2.1.2",
    "nodemon": "^1.17.2",
    "react-test-renderer": "^16.3.0",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1"
  }
}

客户端/package.json:

{
  "name": "curr-calc",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-scripts": "1.1.1"
  },
  "devDependencies": {
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "fetch-mock": "^6.3.0",
    "react-test-renderer": "^16.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000/"
}

编辑:

我尝试手动合并它: 1)我复制了所有依赖项 2)我复制了脚本并重命名了那些重复的脚本,并将“cd client && ”添加到每个客户端脚本的开头 3)我从clien文件夹中删除了package.json、node_modules和所有yarn文件

现在,当我尝试通过yarn dev 运行该应用程序时,我收到:

Listening on port 5000 [1] module.js:549 [1] throw err; [1] ^ [1] [1] Error: Cannot find module '/home/zaba/code/currCalc_react/curr-calc/client/package.json' [1]
at Function.Module._resolveFilename (module.js:547:15) [1] at Function.Module._load (module.js:474:25) [1] at Module.require (module.js:596:17) [1] at require (internal/module.js:11:18) [1]
at getPublicUrl (/home/zaba/code/currCalc_react/curr-calc/node_modules/react-scripts/config/paths.js:34:19) [1] at Object. (/home/zaba/code/currCalc_react/curr-calc/node_modules/react-scripts/config/paths.js:61:14) [1] at Module._compile (module.js:652:30) [1] at Object.Module._extensions..js (module.js:663:10) [1] at Module.load (module.js:565:32) [1] at tryModuleLoad (module.js:505:12) error An unexpected error occurred: "Command failed. [1] Exit code: 1 [1] Command: sh [1] Arguments: -c cd client && react-scripts start [1] Directory: /home/zaba/code/currCalc_react/curr-calc [1] Output: [1] ". info If you think this is a bug, please open a bug report with the information provided in "/home/zaba/code/currCalc_react/curr-calc/yarn-error.log". info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. error An unexpected error occurred: "Command failed. [1] Exit code: 1 [1] Command: sh [1] Arguments: -c yarn start [1] Directory: /home/zaba/code/currCalc_react/curr-calc [1] Output: [1] ". info If you think this is a bug, please open a bug report with the information provided in "/home/zaba/code/currCalc_react/curr-calc/yarn-error.log". info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. [1] yarn client exited with code 1 --> Sending SIGTERM to other processes.. [0] yarn server exited with code null error An unexpected error occurred: "Command failed. Exit code: 1 Command: sh Arguments: -c concurrently --kill-others-on-fail \"yarn server\" \"yarn client\" Directory: /home/zaba/code/currCalc_react/curr-calc Output: ". info If you think this is a bug, please open a bug report with the information provided in "/home/zaba/code/currCalc_react/curr-calc/yarn-error.log".

为什么它仍在寻找 client/package.json 文件?这可能是隐藏在某处的 create-react-app 设置吗?

最佳答案

我不会将它们合并为一个。它们是两个不同的应用程序,具有不同的依赖关系。我会像这样命名名称:@curr-calc/client@curr-calc/server

如果您确实想合并它们,我会通过复制依赖项、devDependency 和脚本来手动完成此操作。删除所有重复项(如果是脚本,您需要重命名那些具有重复键的项)。

关于javascript - 将客户端和服务器 package.json 文件合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49629441/

相关文章:

javascript - 在 JavaScript 中查找调用者脚本?

javascript - 单击时jquery改变颜色

javascript - 高级兄弟选择器

node.js - Ionic 3 Cordova android 构建未显示在平台文件夹中,权限错误

javascript - redux - 如何存储和更新键/值对

javascript - 根据数组关键字计算文本字符串中找到的单词数

node.js - KOA2 中未定义路由器

node.js - 在node.js中自定义验证消息

javascript - 如何在 React 中将 prop 作为 null 传递?

reactjs - 在功能 react 范式中如何跟踪 "frames"之间的对象?