【发布时间】:2017-10-03 18:16:04
【问题描述】:
我创建并发布了我的第一个 npm 包。该包包含 ES6 语法的 JS,并通过 webpack 和 babel 转译为 ES5。
现在我想使用这个 npm 包,但它失败并出现以下错误:
ERROR in ./node_modules/chokidar/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\dev\git\open-source\test\node_modules\chokidar'
@ ./node_modules/chokidar/index.js 3:9-22
@ ./node_modules/watchpack/lib/DirectoryWatcher.js
@ ./node_modules/watchpack/lib/watcherManager.js
@ ./node_modules/watchpack/lib/watchpack.js
@ (webpack)/lib/node/NodeWatchFileSystem.js
@ (webpack)/lib/node/NodeEnvironmentPlugin.js
@ (webpack)/lib/webpack.js
@ ./node_modules/technology-radar/webpack.config.js
@ ./sample/app.js
为了使用,我有以下(最少的)文件集
package.json:
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"webpack": "^3.5.6",
"webpack-dev-server": "^2.4.5",
"babel-polyfill": "^6.26.0",
"html-webpack-plugin": "^2.28.0"
},
"dependencies": {
"d3": "3.5.17",
"technology-radar": "^1.0.3"
}
webpack.config.js
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
module.exports = {
entry: {
sample: './sample/app.js',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
loaders: [
{
loader: 'babel-loader',
include: [
path.resolve(__dirname, "sample")
],
test: /\.js$/
}
]
},
plugins: [
new UglifyJSPlugin(),
new HtmlWebpackPlugin({
template: 'sample/index.html',
filename: 'index.html',
inject: 'body'
})
],
devtool: "#source-map"
};
示例/app.js
import Radar from 'technology-radar';
var radar = new Radar("#techradarContainer");
radar.render();
不知道是在用法还是在npm包本身。 该软件包的源代码可在 github 上的https://github.com/raman-nbg/techradar 获得。 npm 包可在https://www.npmjs.com/package/technology-radar 获得。
应该导出的类(定义为包的 webpack.config.js 中的入口点)定义为 src/Radar.js 和 export default class Radar { ... }。入口点定义为
module.exports = {
entry: {
"technology-radar": "./src/Radar.js",
...
}
...
}
【问题讨论】:
-
这个模块是在节点上使用还是在浏览器上使用?如果它依赖
fs模块才能工作,请考虑在 webpack 配置中添加选项target: 'node'以确保 webpack 忽略fs、path等内置模块。
标签: javascript npm webpack ecmascript-6 es6-modules