我用 create-react-app 开始这个项目,然后弹出。我目前正在分析我的 webpack 包,我得到了这个。
我运行此命令来分析我的包 -
source-map-explorer main.<hash>.js main.<hash>.js.map
我有几个问题-
我在我的应用程序中使用了 moment.js,但是当查看我的 package.json 时,它不存在。它确实存在于我的 yarn.lock 中。为什么会这样?为什么我的应用程序还能运行?
我使用 lodash,但我已经注意导入特定的 lodash 函数(例如,
从“lodash/map”导入 map
)。但是,我在我的 node_module 中看到了 lodash..尽管从未导入过它。为什么这是有效的?是不是因为我的一个依赖项依赖于整个 lodash 包,而我只是运气好?如何获取 source-map-explorer 来分析我的生产构建?我尝试了 CRA 文档中的步骤,但它们出错了。
为什么会出现上述情况?
此处的完整引用是我的 package.json 的 dependencies/devDependencies。
{
"dependencies": {
"autoprefixer": "7.1.6",
"babel-core": "6.26.0",
"babel-eslint": "7.2.3",
"babel-jest": "20.0.3",
"babel-loader": "7.1.2",
"babel-polyfill": "^6.26.0",
"babel-preset-react-app": "^3.1.0",
"babel-runtime": "6.26.0",
"case-sensitive-paths-webpack-plugin": "2.1.1",
"chalk": "1.1.3",
"cross-env": "5.1.4",
"css-loader": "0.28.7",
"dotenv": "4.0.0",
"eslint": "4.10.0",
"eslint-config-react-app": "^2.0.1",
"eslint-loader": "1.9.0",
"eslint-plugin-flowtype": "2.39.1",
"eslint-plugin-import": "2.8.0",
"eslint-plugin-jsx-a11y": "5.1.1",
"eslint-plugin-react": "7.4.0",
"extract-text-webpack-plugin": "3.0.2",
"file-loader": "1.1.5",
"fs-extra": "3.0.1",
"html-webpack-plugin": "2.29.0",
"jest": "20.0.4",
"object-assign": "4.1.1",
"postcss-flexbugs-fixes": "3.2.0",
"postcss-loader": "2.0.8",
"promise": "8.0.1",
"query-string": "^5.1.0",
"raf": "3.4.0",
"react": "^16.2.0",
"react-collapse": "^4.0.3",
"react-countdown-now": "^1.3.0",
"react-dev-utils": "^4.2.1",
"react-dom": "^16.2.0",
"react-grecaptcha": "^1.2.5",
"react-md-spinner": "^0.2.5",
"react-motion": "^0.5.2",
"react-player": "^1.1.1",
"react-redux": "^5.0.6",
"react-responsive": "^4.0.3",
"react-router-dom": "^4.2.2",
"react-router-hash-link": "^1.2.0",
"react-select": "^1.2.1",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"style-loader": "0.19.0",
"sw-precache-webpack-plugin": "0.11.4",
"url-loader": "0.6.2",
"webpack": "3.8.1",
"webpack-dev-server": "2.9.4",
"webpack-manifest-plugin": "1.3.2",
"whatwg-fetch": "2.0.3",
"xelpmoc-core": "^0.3.1"
},
"devDependencies": {
"compression-webpack-plugin": "^1.1.3",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.14.0",
"eslint-config-recommended": "^2.0.0",
"eslint-plugin-react": "^7.5.1",
"google-map-react": "^0.34.0",
"husky": "^0.14.3",
"lint-staged": "^6.0.0",
"pm2": "^2.9.1",
"prettier": "^1.9.2",
"redux-mock-store": "^1.4.0"
},
}
最佳答案
yarn.lock 是一个“锁文件”,它锁定了你的依赖树,这样当另一个队友(或 CI 服务器)运行构建时,它会获得相同的依赖(因此是相同的构建)。锁定文件不仅列出了依赖关系,还列出了 deps 的 deps。
NPM/yarn 尽可能平展 node_modules 中的依赖项,因此您可能有一些正在安装 lodash/moment 的依赖项。您可以通过运行 npm ls lodash
来调试它以查看它的来源。您绝对应该安装那些您正在使用但“意外”获得的软件包。
我无法回答#3
关于javascript - 为什么包在我的 yarn.lock 中而不在我的 package.json 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49743302/