尽管存在于 node_modules 中,但未定义 peerDependency
peerDependency is not defined despite it being present in node_modules
我正在学习构建 NPM 库。可以找到它的源代码 HERE。我已将 lodash
作为 peerDependency,以便应用程序(使用该库)可以自行安装它。
现在的问题是,当我在应用程序中安装库 (@a6kme/math
) 时,导入库时出现错误 Unhandled Rejection (ReferenceError): lodash is not defined
。我已经检查过,应用程序已经通过其他一些库安装了 lodash。 (lodash
存在于 node_modules
文件夹中)
=== 来自 CODE REPOSITORY 的一些文件 ===
我的package.json
{
"name": "@a6kme/math",
"version": "1.0.5",
"description": "",
"main": "dist/math.js",
"scripts": {
"test": "jest",
"build": "webpack --mode=production",
"prepare": "npm run test",
"posttest": "npm run build"
},
"files": [
"/dist"
],
"repository": {
"type": "git",
"url": "https://github.com/a6kme/math.git"
},
"keywords": [
"webpack",
"webpack-library",
"bundling",
"library"
],
"author": "a6kme",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
"eslint": "^5.3.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-prettier": "^3.0.1",
"jest": "^24.7.1",
"lodash": "^4.17.11",
"prettier": "1.17.0",
"webpack": "^4.30.0",
"webpack-cli": "^3.3.0"
},
"peerDependencies": {
"lodash": "*"
}
}
我的webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'math.js',
library: 'mathJs'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
externals: [/^lodash\/.+$/]
};
我的.babelrc
{
"presets": ["@babel/preset-env"]
}
我在代码库中创建了一个 github issue。
请给我一些方向看。
错误与我尝试构建库的方式有关。 webpack.config.js
中未设置 libraryTarget。配置原来是
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'math.js',
library: 'mathJs'
}
虽然应该是
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'math.js',
library: 'mathJs',
libraryTarget: 'umd',
globalObject: 'this'
}
libraryTarget: 'umd' - This exposes your library under all the module definitions, allowing it to work with CommonJS, AMD and as global variable.
由于这是我最初的要求,我的库应该可以通过 es6 import
或作为浏览器中的 script
标签使用。
我正在学习构建 NPM 库。可以找到它的源代码 HERE。我已将 lodash
作为 peerDependency,以便应用程序(使用该库)可以自行安装它。
现在的问题是,当我在应用程序中安装库 (@a6kme/math
) 时,导入库时出现错误 Unhandled Rejection (ReferenceError): lodash is not defined
。我已经检查过,应用程序已经通过其他一些库安装了 lodash。 (lodash
存在于 node_modules
文件夹中)
=== 来自 CODE REPOSITORY 的一些文件 ===
我的package.json
{
"name": "@a6kme/math",
"version": "1.0.5",
"description": "",
"main": "dist/math.js",
"scripts": {
"test": "jest",
"build": "webpack --mode=production",
"prepare": "npm run test",
"posttest": "npm run build"
},
"files": [
"/dist"
],
"repository": {
"type": "git",
"url": "https://github.com/a6kme/math.git"
},
"keywords": [
"webpack",
"webpack-library",
"bundling",
"library"
],
"author": "a6kme",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
"eslint": "^5.3.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-prettier": "^3.0.1",
"jest": "^24.7.1",
"lodash": "^4.17.11",
"prettier": "1.17.0",
"webpack": "^4.30.0",
"webpack-cli": "^3.3.0"
},
"peerDependencies": {
"lodash": "*"
}
}
我的webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'math.js',
library: 'mathJs'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
externals: [/^lodash\/.+$/]
};
我的.babelrc
{
"presets": ["@babel/preset-env"]
}
我在代码库中创建了一个 github issue。 请给我一些方向看。
错误与我尝试构建库的方式有关。 webpack.config.js
中未设置 libraryTarget。配置原来是
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'math.js',
library: 'mathJs'
}
虽然应该是
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'math.js',
library: 'mathJs',
libraryTarget: 'umd',
globalObject: 'this'
}
libraryTarget: 'umd' - This exposes your library under all the module definitions, allowing it to work with CommonJS, AMD and as global variable.
由于这是我最初的要求,我的库应该可以通过 es6 import
或作为浏览器中的 script
标签使用。