我目前正在为NodeJS项目使用webpack,但遇到一个奇怪的问题,我无法弄清。
运行节点捆绑包时,出现此错误:
internal/modules/cjs/loader.js:596
throw err;
^
Error: Cannot find module 'json-stringify-safe'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
at Function.Module._load (internal/modules/cjs/loader.js:520:25)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/private/tmp/dist/node_modules/newrelic/lib/util/logger.js:3:17)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
是的,该模块丢失了。有趣的是,在捆绑包中,模块在那里:
/***/ "./node_modules/json-stringify-safe/stringify.js":
/*!*******************************************************!*\
!*** ./node_modules/json-stringify-safe/stringify.js ***!
\*******************************************************/
/*! no static exports found */
/***/ (function(module, exports) {
exports = module.exports = stringify
exports.getSerialize = serializer
function stringify(obj, replacer, spaces, cycleReplacer) {
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
}
function serializer(replacer, cycleReplacer) {
var stack = [], keys = []
if (cycleReplacer == null) cycleReplacer = function(key, value) {
if (stack[0] === value) return "[Circular ~]"
return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]"
}
return function(key, value) {
if (stack.length > 0) {
var thisPos = stack.indexOf(this)
~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value)
}
else stack.push(value)
return replacer == null ? value : replacer.call(this, key, value)
}
}
/***/ }),
如果我有webpack手动将模块复制到(复制到node_modules中),此错误消失,然后是另一个模块...然后是另一个...等等...
有人知道为什么找不到捆绑包中的模块吗?
这是我的基本Webpack配置:
import CleanWebpackPlugin from 'clean-webpack-plugin';
import path from 'path';
import webpack from 'webpack';
import { removeEmpty } from 'webpack-config-utils';
const distDir = path.resolve(__dirname, 'dist');
const contextDir = path.resolve(__dirname);
export const copyThings = [
{ from: 'start.sh', to: 'start.sh' },
{ from: 'ecosystem.config.js', to: 'ecosystem.config.js' },
{ from: 'newrelic_prod.js', to: 'newrelic.js' },
{ from: 'node_modules/newrelic', to: 'node_modules/newrelic' },
{
from: 'node_modules/apollo-engine-binary-darwin',
to: 'node_modules/apollo-engine-binary-darwin'
},
{
from: 'node_modules/apollo-engine-binary-linux',
to: 'node_modules/apollo-engine-binary-linux'
}
];
const common: webpack.Configuration = {
context: contextDir,
entry: './src/server.ts',
output: { filename: 'tank.js', path: distDir },
module: {
rules: [
{ test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/ },
{ test: /\.mjs$/, include: /node_modules/, type: 'javascript/auto' }
]
},
resolve: { extensions: ['.ts', '.js', '.mjs'] },
target: 'node',
externals: [
function(context, request, callback) {
if (request.indexOf('apollo-engine-binary') > -1) {
return callback(undefined, 'commonjs ' + request);
}
callback(undefined, undefined);
},
function(context, request, callback) {
if (request.indexOf('newrelic') > -1) {
return callback(undefined, 'commonjs ' + request);
}
callback(undefined, undefined);
}
],
plugins: removeEmpty([
new CleanWebpackPlugin([distDir], {
root: contextDir
}),
new webpack.IgnorePlugin(/vertx/),
new webpack.IgnorePlugin(/bufferutil/),
new webpack.IgnorePlugin(/utf-8-validate/)
])
};
export default common;
以及我的环境特定配置:
import CopyWebpackPlugin from 'copy-webpack-plugin';
import merge from 'webpack-merge';
import common, { copyThings } from './webpack.common';
const config = merge(common, {
devtool: 'inline-source-map',
mode: 'development',
plugins: [new CopyWebpackPlugin(copyThings)]
});
export default config;