question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

proxy server config error::: Must use import to load ES Module

See original GitHub issue

Description

Crashing when running npm start with setupProxy.js I configured proxy server manually, created setupProxy.js in my src folder. The problem is that I’m using new ES6 modules in node.js and when I’m running npm start I get this error:

Must use import to load ES Module: C:\Users\d\projects\v\src\setupProxy.js require() of ES modules is not supported. require() of C:\Users\d\projects\v\src\setupProxy.js from C:\Users\d\projects \v\node_modules\react-scripts\config\webpackDevServer.config.js is an ES module file as it is a .js file whose nearest parent package.json contains “type”: “module” which defines all .js files in that package scope as ES modules. Instead rename setupProxy.js to end in .cjs, change the requiring code to use import(), or remove “type”: "module " from C:\Users\d\projects\v\package.json.

Proxy config:

import { createRequire } from ‘module’;

const require = createRequire(import.meta.url);

const proxy = require(‘http-proxy-middleware’);

const target = process.env.PROXY_TARGET;

if (!target) { throw new Error(‘Please specify the proxy target in your environment variables.’); }

export default (app) => { app.use( ‘/api/v1’, proxy({ target, changeOrigin: true, secure: false, }), ); };

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

  • create-react-app proxy config
  • create-react-app Must use import to load ES Module
  • webpack Must use import to load ES Module

Environment

System: OS: Windows 10 10.0.17763 CPU: (8) x64 Intel® Core™ i7-8650U CPU @ 1.90GHz Binaries: Node: 14.14.0 - C:\Program Files\nodejs\node.EXE Yarn: Not Found npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: 83.0.4103.61 Edge: Spartan (44.17763.831.0) Internet Explorer: 11.0.17763.771 npmPackages: react: ~16.13.1 => 16.13.1 react-dom: ~16.13.1 => 16.13.1 react-scripts: 4.0.0 => 4.0.0 npmGlobalPackages: create-react-app: Not Found

Steps to reproduce

  1. install node.js 14+
  2. set “type”: “module” in your package.json
  3. config a proxy server using setupProxy.js file
  4. run npm start

Expected behavior

Should run dev server with proxy

Actual behavior

crashing with error

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
gegham1commented, Nov 23, 2020

I created a new project and could successfully reproduced the BUG. You can clone it and try https://github.com/gegham1/react-scripts-bug

3reactions
gegham1commented, Dec 28, 2020

Hey guys any workarounds for this ??

Read more comments on GitHub >

github_iconTop Results From Across the Web

Importing in Node.js: error "Must use import to load ES Module"
Use version 2: npm install node-fetch@2. node-fetch from v3 is an ESM-only module - you are not able to import it with require()....
Read more >
Error ERR REQUIRE ESM | Must use import to load ES Module
Your browser can't play this video. Learn more. Switch camera.
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Absolute specifiers like 'file:///opt/nodejs/config.js' . ... Instead, use import() to load an ES module from a CommonJS module.
Read more >
import - JavaScript - MDN Web Docs - Mozilla
Must be a valid JavaScript identifier. module-name ... To load modules in non-module contexts, use the dynamic import syntax instead.
Read more >
error [err_require_esm]: require() of es module nanoid
js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^ Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /root/PVWBOT/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found