[英]How to import node module or JavaScript file without webpack dependency when using create-react-app?
I'm trying to include a separate JavaScript file (server.js) from my index.js file using create-react-app. 我正在尝试使用create-react-app从我的index.js文件中包含一个单独的JavaScript文件(server.js)。 This server.js file uses the
npm zeromq
node module which doesn't seem to play well with webpack. 这个server.js文件使用的是
npm zeromq
节点模块,它似乎与webpack不兼容。 Because of this, I'm either trying to exclude the zeromq
node module or server.js. 因此,我要么试图排除
zeromq
节点模块或server.js。
I'm made attempts to edit webpack.config.js with no luck. 我试图编辑webpack.config.js没有运气。 Some of what I I've tried:
我尝试过的一些内容:
{
...
test: /\.(js|mjs|jsx)$/,
exclude: /server\.js$/,
...
}
Unfortunately all the examples I have found look nothing like the config file I currently have. 不幸的是,我发现的所有示例看起来都不像我目前的配置文件。 My generated config from project/node_modules/webpack/config looks something like this...
我从project / node_modules / webpack / config生成的配置看起来像这样......
...
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
// This is the production and development configuration.
// It is focused on developer experience, fast rebuilds, and a minimal bundle.
module.exports = function(webpackEnv) {
const isEnvDevelopment = webpackEnv === 'development';
const isEnvProduction = webpackEnv === 'production';
...
I import the server.js file from index.js in a standard way. 我以标准方式从index.js导入server.js文件。
import './server.js'
So it's possible that your issue is that you just need to have 所以您的问题可能就是您需要的问题
import server from './server.js';
Or whatever specific module it is you need. 或者您需要的任何特定模块。 That being said, if you are having issues with something being incompatible with webpack it might be worth trying a require statement instead.
话虽这么说,如果你遇到与webpack不兼容的问题,可能值得尝试一下require语句。 So something like:
所以类似于:
const server = require('./server.js');
Also have you ejected your react app? 还有你弹出你的反应应用程序? It's often easier to modify parts of your build scripts if you've ejected an app created with create-react-app.
如果您弹出了使用create-react-app创建的应用程序,则通常可以更轻松地修改部分构建脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.