简体   繁体   English

使用create-react-app时如何在没有webpack依赖的情况下导入节点模块或JavaScript文件?

[英]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.

相关问题 在使用 create-react-app 创建的 reactjs 应用程序中导入 node_module dist js 文件 - Import a node_module dist js file in reactjs application created using create-react-app 当我使用带有 javascript 的 create-react-app 时如何导入 md 文件? - how to import md file when I use create-react-app with javascript? 如何在不弹出的情况下将 Webpack 模块联合插件与 create-react-app 一起使用 - How to use Webpack Module Federation plugin with create-react-app without ejecting 如何在 create-react-app 中导入自制模块? - How to import a self-made module in create-react-app? Node + create-react-app + Express - 如何将函数导入 Express - Node + create-react-app + Express - how to import function into Express 使用 create-react-app 从 github 导入 JSX 文件时找不到模块 - Module not found when importing JSX file off github using create-react-app 如何在不使用 create-react-app 且仅使用我自己的 Webpack 的情况下设置 package.json 进行部署? - How to set the package.json for deployment without using create-react-app and only using my own Webpack? 有没有办法在不弹出的情况下为 create-react-app 更改 webpack 的 eslint - Is there a way to change the eslint of the webpack for the create-react-app without ejecting 使用 create-react-app 时 .babelrc 文件的位置是什么? - What is the location of .babelrc file when using create-react-app? 有没有办法在没有 create-react-app 的情况下在 React 中导入模块? - Is there a way to import modules in React without create-react-app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM