Share model between backend and frontend when using create-react-app Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceThe create-react-app imports restriction outside of src directoryGetting an error when using babel to convert react jsx codeMoonMail-UI cannot build properly using reactjs / web-packbabel-preset-react-app not picking up environment variablesThere are multiple modules with names that only differ in casing ReactWebpack compiling throwing error with mobxnpm start (or yarn start) error with babel when creating react applicationsError while parsing JSON - Unexpected token } in JSON at position 450How to fix 'Plugin/Preset files are not allowed to export objects, only functions.' on 'npm run' when setting up Webpack for React?ReactJS: unexpected token '<'React Native for Android - Using ./gradlew assembleRelease will produce “Cannot find module 'babel-preset-react-app' “ error
Notation for two qubit composite product state
How to market an anarchic city as a tourism spot to people living in civilized areas?
Using "nakedly" instead of "with nothing on"
What LEGO pieces have "real-world" functionality?
Geometric mean and geometric standard deviation
Stop battery usage [Ubuntu 18]
Is there a service that would inform me whenever a new direct route is scheduled from a given airport?
When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?
Are my PIs rude or am I just being too sensitive?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Simulating Exploding Dice
What do you call the holes in a flute?
What is the electric potential inside a point charge?
How many things? AとBがふたつ
How are presidential pardons supposed to be used?
Windows 10: How to Lock (not sleep) laptop on lid close?
Complexity of many constant time steps with occasional logarithmic steps
Replacing HDD with SSD; what about non-APFS/APFS?
Why does tar appear to skip file contents when output file is /dev/null?
Active filter with series inductor and resistor - do these exist?
Mortgage adviser recommends a longer term than necessary combined with overpayments
How to stop my camera from exagerrating differences in skin colour?
Estimated State payment too big --> money back; + 2018 Tax Reform
Need a suitable toxic chemical for a murder plot in my novel
Share model between backend and frontend when using create-react-app
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceThe create-react-app imports restriction outside of src directoryGetting an error when using babel to convert react jsx codeMoonMail-UI cannot build properly using reactjs / web-packbabel-preset-react-app not picking up environment variablesThere are multiple modules with names that only differ in casing ReactWebpack compiling throwing error with mobxnpm start (or yarn start) error with babel when creating react applicationsError while parsing JSON - Unexpected token } in JSON at position 450How to fix 'Plugin/Preset files are not allowed to export objects, only functions.' on 'npm run' when setting up Webpack for React?ReactJS: unexpected token '<'React Native for Android - Using ./gradlew assembleRelease will produce “Cannot find module 'babel-preset-react-app' “ error
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I created a project using create-react-app. The front-end is described inside the ./src
folder. I have a file named server.js
on the root of the project. This file is describing the API using express.
I would like to have a folder with models that would be shared between the front-end (under ./src
) and the back-end (under ./server.js
).
An example of a one of the classes that I would like to share:
export default class DataModel
constructor(name)
this.name = name;
If I put this class under ./src/models/DataModel.js
, I can use it inside ./src
by using import DataModel from '../models/DataModel';
but I can't use it inside ./server.js
as it gives me the following error:
Unexpected token export
And I can't put the class directly under to the root of my project as create-react-app do not accept import from outside the ./src
folder and will give me the following error:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update
I tried using react-app-rewired
but without success.
My package.json has been updated:
//...
"scripts":
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
,
"devDependencies":
"react-app-rewired": "^2.1.0"
And I added the file config-overrides.js
on the root of my project (same level as package.json and my DataModel class).
/* config-overrides.js */
module.exports = function override(config, env)
delete config.resolve.plugins.ModuleScopePlugin;
return config;
But I still have the same issue:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update #2
I logged the config
that is passed through the override
function and here is what I got:
"mode": "development",
"devtool": "cheap-module-source-map",
"entry": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\webpackHotDevClient.js",
"C:\Users\me\Documents\nodejs\MY_PROJECT\src\index.js"
],
"output":
"pathinfo": true,
"filename": "static/js/bundle.js",
"chunkFilename": "static/js/[name].chunk.js",
"publicPath": "/"
,
"optimization":
"splitChunks":
"chunks": "all",
"name": false
,
"runtimeChunk": true
,
"resolve":
"modules": [
"node_modules"
],
"extensions": [
".web.mjs",
".mjs",
".web.js",
".js",
".json",
".web.jsx",
".jsx"
],
"alias":
"react-native": "react-native-web"
,
"plugins": [
"topLevelLoader":
,
"appSrcs": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\src"
],
"allowedFiles":
]
,
"resolveLoader":
"plugins": [
]
,
"module":
"strictExportPresence": true,
"rules": [
"parser":
"requireEnsure": false
,
"test": ,
"enforce": "pre",
"use": [
"options":
"formatter": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\eslintFormatter.js",
"eslintPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint\lib\api.js",
"baseConfig":
"extends": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-config-react-app\index.js"
],
"settings":
"react":
"version": "999.999.999"
,
"ignore": false,
"useEslintrc": false
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-loader\index.js"
],
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src"
,
"oneOf": [
"test": [
,
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\url-loader\dist\cjs.js",
"options":
"limit": 10000,
"name": "static/media/[name].[hash:8].[ext]"
,
"test": ,
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"customize": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\webpack-overrides.js",
"babelrc": false,
"configFile": false,
"presets": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\index.js"
],
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"plugins": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-plugin-named-asset-import\index.js",
"loaderMap":
"svg":
"ReactComponent": "@svgr/webpack?-prettier,-svgo![path]"
]
],
"cacheDirectory": true,
"cacheCompression": false
,
"test": ,
"exclude": ,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"babelrc": false,
"configFile": false,
"compact": false,
"presets": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\dependencies.js",
"helpers": true
]
],
"cacheDirectory": true,
"cacheCompression": false,
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"sourceMaps": false
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"exclude": [
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\file-loader\dist\cjs.js",
"options":
"name": "static/media/[name].[hash:8].[ext]"
]
]
,
"plugins": [
"options":
"template": "C:\Users\me\Documents\nodejs\MY_PROJECT\public\index.html",
"templateContent": false,
"filename": "index.html",
"hash": false,
"inject": true,
"compile": true,
"favicon": false,
"minify": false,
"cache": true,
"showErrors": true,
"chunks": "all",
"excludeChunks": [],
"chunksSortMode": "auto",
"meta": ,
"title": "Webpack App",
"xhtml": false
,
"version": 4
,
"replacements":
"NODE_ENV": "development",
"PUBLIC_URL": "",
"REACT_APP_DEFAULT_LANGUAGE": "fr"
,
"appPath": "C:\Users\me\Documents\nodejs\MY_PROJECT"
,
"definitions":
"process.env":
"NODE_ENV": ""development"",
"PUBLIC_URL": """",
"REACT_APP_DEFAULT_LANGUAGE": ""fr""
,
"options": ,
"fullBuildTimeout": 200,
"requestTimeout": 10000
,
"options": ,
"pathCache": ,
"fsOperations": 0,
"primed": false
,
"nodeModulesPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules"
,
"options":
"resourceRegExp": ,
"contextRegExp":
,
"opts":
"publicPath": "/",
"basePath": "",
"fileName": "asset-manifest.json",
"transformExtensions": ,
"writeToFileEmit": false,
"seed": null,
"filter": null,
"map": null,
"generate": null,
"sort": null
],
"node":
"dgram": "empty",
"fs": "empty",
"net": "empty",
"tls": "empty",
"child_process": "empty"
,
"performance": false
I log it before calling the delete
operation and as we can see their is no ModuleScopePlugin
. So why does it still failing?
Any help is welcome.
Thanks.
reactjs model-view-controller
add a comment |
I created a project using create-react-app. The front-end is described inside the ./src
folder. I have a file named server.js
on the root of the project. This file is describing the API using express.
I would like to have a folder with models that would be shared between the front-end (under ./src
) and the back-end (under ./server.js
).
An example of a one of the classes that I would like to share:
export default class DataModel
constructor(name)
this.name = name;
If I put this class under ./src/models/DataModel.js
, I can use it inside ./src
by using import DataModel from '../models/DataModel';
but I can't use it inside ./server.js
as it gives me the following error:
Unexpected token export
And I can't put the class directly under to the root of my project as create-react-app do not accept import from outside the ./src
folder and will give me the following error:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update
I tried using react-app-rewired
but without success.
My package.json has been updated:
//...
"scripts":
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
,
"devDependencies":
"react-app-rewired": "^2.1.0"
And I added the file config-overrides.js
on the root of my project (same level as package.json and my DataModel class).
/* config-overrides.js */
module.exports = function override(config, env)
delete config.resolve.plugins.ModuleScopePlugin;
return config;
But I still have the same issue:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update #2
I logged the config
that is passed through the override
function and here is what I got:
"mode": "development",
"devtool": "cheap-module-source-map",
"entry": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\webpackHotDevClient.js",
"C:\Users\me\Documents\nodejs\MY_PROJECT\src\index.js"
],
"output":
"pathinfo": true,
"filename": "static/js/bundle.js",
"chunkFilename": "static/js/[name].chunk.js",
"publicPath": "/"
,
"optimization":
"splitChunks":
"chunks": "all",
"name": false
,
"runtimeChunk": true
,
"resolve":
"modules": [
"node_modules"
],
"extensions": [
".web.mjs",
".mjs",
".web.js",
".js",
".json",
".web.jsx",
".jsx"
],
"alias":
"react-native": "react-native-web"
,
"plugins": [
"topLevelLoader":
,
"appSrcs": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\src"
],
"allowedFiles":
]
,
"resolveLoader":
"plugins": [
]
,
"module":
"strictExportPresence": true,
"rules": [
"parser":
"requireEnsure": false
,
"test": ,
"enforce": "pre",
"use": [
"options":
"formatter": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\eslintFormatter.js",
"eslintPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint\lib\api.js",
"baseConfig":
"extends": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-config-react-app\index.js"
],
"settings":
"react":
"version": "999.999.999"
,
"ignore": false,
"useEslintrc": false
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-loader\index.js"
],
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src"
,
"oneOf": [
"test": [
,
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\url-loader\dist\cjs.js",
"options":
"limit": 10000,
"name": "static/media/[name].[hash:8].[ext]"
,
"test": ,
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"customize": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\webpack-overrides.js",
"babelrc": false,
"configFile": false,
"presets": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\index.js"
],
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"plugins": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-plugin-named-asset-import\index.js",
"loaderMap":
"svg":
"ReactComponent": "@svgr/webpack?-prettier,-svgo![path]"
]
],
"cacheDirectory": true,
"cacheCompression": false
,
"test": ,
"exclude": ,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"babelrc": false,
"configFile": false,
"compact": false,
"presets": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\dependencies.js",
"helpers": true
]
],
"cacheDirectory": true,
"cacheCompression": false,
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"sourceMaps": false
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"exclude": [
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\file-loader\dist\cjs.js",
"options":
"name": "static/media/[name].[hash:8].[ext]"
]
]
,
"plugins": [
"options":
"template": "C:\Users\me\Documents\nodejs\MY_PROJECT\public\index.html",
"templateContent": false,
"filename": "index.html",
"hash": false,
"inject": true,
"compile": true,
"favicon": false,
"minify": false,
"cache": true,
"showErrors": true,
"chunks": "all",
"excludeChunks": [],
"chunksSortMode": "auto",
"meta": ,
"title": "Webpack App",
"xhtml": false
,
"version": 4
,
"replacements":
"NODE_ENV": "development",
"PUBLIC_URL": "",
"REACT_APP_DEFAULT_LANGUAGE": "fr"
,
"appPath": "C:\Users\me\Documents\nodejs\MY_PROJECT"
,
"definitions":
"process.env":
"NODE_ENV": ""development"",
"PUBLIC_URL": """",
"REACT_APP_DEFAULT_LANGUAGE": ""fr""
,
"options": ,
"fullBuildTimeout": 200,
"requestTimeout": 10000
,
"options": ,
"pathCache": ,
"fsOperations": 0,
"primed": false
,
"nodeModulesPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules"
,
"options":
"resourceRegExp": ,
"contextRegExp":
,
"opts":
"publicPath": "/",
"basePath": "",
"fileName": "asset-manifest.json",
"transformExtensions": ,
"writeToFileEmit": false,
"seed": null,
"filter": null,
"map": null,
"generate": null,
"sort": null
],
"node":
"dgram": "empty",
"fs": "empty",
"net": "empty",
"tls": "empty",
"child_process": "empty"
,
"performance": false
I log it before calling the delete
operation and as we can see their is no ModuleScopePlugin
. So why does it still failing?
Any help is welcome.
Thanks.
reactjs model-view-controller
stackoverflow.com/questions/44114436/… . And use CommonJS exports instead of ES modules.
– estus
Feb 27 at 13:07
But it require to do aneject
. What if I do not want to eject my app?
– MHogge
Feb 27 at 13:35
1
You may use react-app-rewired. It's either one or another. A more generalized way is to make privatecommon
package and use it in both.
– estus
Feb 27 at 13:44
I didn't know about react-app-rewired (I'm new to react). I'll try it soon, thanks.
– MHogge
Feb 27 at 13:47
I triedreact-app-rewired
but it doesn't seem to be working. Any idea of what I'm doing wrong?
– MHogge
Mar 8 at 14:47
add a comment |
I created a project using create-react-app. The front-end is described inside the ./src
folder. I have a file named server.js
on the root of the project. This file is describing the API using express.
I would like to have a folder with models that would be shared between the front-end (under ./src
) and the back-end (under ./server.js
).
An example of a one of the classes that I would like to share:
export default class DataModel
constructor(name)
this.name = name;
If I put this class under ./src/models/DataModel.js
, I can use it inside ./src
by using import DataModel from '../models/DataModel';
but I can't use it inside ./server.js
as it gives me the following error:
Unexpected token export
And I can't put the class directly under to the root of my project as create-react-app do not accept import from outside the ./src
folder and will give me the following error:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update
I tried using react-app-rewired
but without success.
My package.json has been updated:
//...
"scripts":
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
,
"devDependencies":
"react-app-rewired": "^2.1.0"
And I added the file config-overrides.js
on the root of my project (same level as package.json and my DataModel class).
/* config-overrides.js */
module.exports = function override(config, env)
delete config.resolve.plugins.ModuleScopePlugin;
return config;
But I still have the same issue:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update #2
I logged the config
that is passed through the override
function and here is what I got:
"mode": "development",
"devtool": "cheap-module-source-map",
"entry": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\webpackHotDevClient.js",
"C:\Users\me\Documents\nodejs\MY_PROJECT\src\index.js"
],
"output":
"pathinfo": true,
"filename": "static/js/bundle.js",
"chunkFilename": "static/js/[name].chunk.js",
"publicPath": "/"
,
"optimization":
"splitChunks":
"chunks": "all",
"name": false
,
"runtimeChunk": true
,
"resolve":
"modules": [
"node_modules"
],
"extensions": [
".web.mjs",
".mjs",
".web.js",
".js",
".json",
".web.jsx",
".jsx"
],
"alias":
"react-native": "react-native-web"
,
"plugins": [
"topLevelLoader":
,
"appSrcs": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\src"
],
"allowedFiles":
]
,
"resolveLoader":
"plugins": [
]
,
"module":
"strictExportPresence": true,
"rules": [
"parser":
"requireEnsure": false
,
"test": ,
"enforce": "pre",
"use": [
"options":
"formatter": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\eslintFormatter.js",
"eslintPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint\lib\api.js",
"baseConfig":
"extends": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-config-react-app\index.js"
],
"settings":
"react":
"version": "999.999.999"
,
"ignore": false,
"useEslintrc": false
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-loader\index.js"
],
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src"
,
"oneOf": [
"test": [
,
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\url-loader\dist\cjs.js",
"options":
"limit": 10000,
"name": "static/media/[name].[hash:8].[ext]"
,
"test": ,
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"customize": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\webpack-overrides.js",
"babelrc": false,
"configFile": false,
"presets": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\index.js"
],
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"plugins": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-plugin-named-asset-import\index.js",
"loaderMap":
"svg":
"ReactComponent": "@svgr/webpack?-prettier,-svgo![path]"
]
],
"cacheDirectory": true,
"cacheCompression": false
,
"test": ,
"exclude": ,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"babelrc": false,
"configFile": false,
"compact": false,
"presets": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\dependencies.js",
"helpers": true
]
],
"cacheDirectory": true,
"cacheCompression": false,
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"sourceMaps": false
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"exclude": [
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\file-loader\dist\cjs.js",
"options":
"name": "static/media/[name].[hash:8].[ext]"
]
]
,
"plugins": [
"options":
"template": "C:\Users\me\Documents\nodejs\MY_PROJECT\public\index.html",
"templateContent": false,
"filename": "index.html",
"hash": false,
"inject": true,
"compile": true,
"favicon": false,
"minify": false,
"cache": true,
"showErrors": true,
"chunks": "all",
"excludeChunks": [],
"chunksSortMode": "auto",
"meta": ,
"title": "Webpack App",
"xhtml": false
,
"version": 4
,
"replacements":
"NODE_ENV": "development",
"PUBLIC_URL": "",
"REACT_APP_DEFAULT_LANGUAGE": "fr"
,
"appPath": "C:\Users\me\Documents\nodejs\MY_PROJECT"
,
"definitions":
"process.env":
"NODE_ENV": ""development"",
"PUBLIC_URL": """",
"REACT_APP_DEFAULT_LANGUAGE": ""fr""
,
"options": ,
"fullBuildTimeout": 200,
"requestTimeout": 10000
,
"options": ,
"pathCache": ,
"fsOperations": 0,
"primed": false
,
"nodeModulesPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules"
,
"options":
"resourceRegExp": ,
"contextRegExp":
,
"opts":
"publicPath": "/",
"basePath": "",
"fileName": "asset-manifest.json",
"transformExtensions": ,
"writeToFileEmit": false,
"seed": null,
"filter": null,
"map": null,
"generate": null,
"sort": null
],
"node":
"dgram": "empty",
"fs": "empty",
"net": "empty",
"tls": "empty",
"child_process": "empty"
,
"performance": false
I log it before calling the delete
operation and as we can see their is no ModuleScopePlugin
. So why does it still failing?
Any help is welcome.
Thanks.
reactjs model-view-controller
I created a project using create-react-app. The front-end is described inside the ./src
folder. I have a file named server.js
on the root of the project. This file is describing the API using express.
I would like to have a folder with models that would be shared between the front-end (under ./src
) and the back-end (under ./server.js
).
An example of a one of the classes that I would like to share:
export default class DataModel
constructor(name)
this.name = name;
If I put this class under ./src/models/DataModel.js
, I can use it inside ./src
by using import DataModel from '../models/DataModel';
but I can't use it inside ./server.js
as it gives me the following error:
Unexpected token export
And I can't put the class directly under to the root of my project as create-react-app do not accept import from outside the ./src
folder and will give me the following error:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update
I tried using react-app-rewired
but without success.
My package.json has been updated:
//...
"scripts":
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
,
"devDependencies":
"react-app-rewired": "^2.1.0"
And I added the file config-overrides.js
on the root of my project (same level as package.json and my DataModel class).
/* config-overrides.js */
module.exports = function override(config, env)
delete config.resolve.plugins.ModuleScopePlugin;
return config;
But I still have the same issue:
Module not found: You attempted to import ../../DataModel which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
Update #2
I logged the config
that is passed through the override
function and here is what I got:
"mode": "development",
"devtool": "cheap-module-source-map",
"entry": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\webpackHotDevClient.js",
"C:\Users\me\Documents\nodejs\MY_PROJECT\src\index.js"
],
"output":
"pathinfo": true,
"filename": "static/js/bundle.js",
"chunkFilename": "static/js/[name].chunk.js",
"publicPath": "/"
,
"optimization":
"splitChunks":
"chunks": "all",
"name": false
,
"runtimeChunk": true
,
"resolve":
"modules": [
"node_modules"
],
"extensions": [
".web.mjs",
".mjs",
".web.js",
".js",
".json",
".web.jsx",
".jsx"
],
"alias":
"react-native": "react-native-web"
,
"plugins": [
"topLevelLoader":
,
"appSrcs": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\src"
],
"allowedFiles":
]
,
"resolveLoader":
"plugins": [
]
,
"module":
"strictExportPresence": true,
"rules": [
"parser":
"requireEnsure": false
,
"test": ,
"enforce": "pre",
"use": [
"options":
"formatter": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\react-dev-utils\eslintFormatter.js",
"eslintPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint\lib\api.js",
"baseConfig":
"extends": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-config-react-app\index.js"
],
"settings":
"react":
"version": "999.999.999"
,
"ignore": false,
"useEslintrc": false
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\eslint-loader\index.js"
],
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src"
,
"oneOf": [
"test": [
,
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\url-loader\dist\cjs.js",
"options":
"limit": 10000,
"name": "static/media/[name].[hash:8].[ext]"
,
"test": ,
"include": "C:\Users\me\Documents\nodejs\MY_PROJECT\src",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"customize": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\webpack-overrides.js",
"babelrc": false,
"configFile": false,
"presets": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\index.js"
],
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"plugins": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-plugin-named-asset-import\index.js",
"loaderMap":
"svg":
"ReactComponent": "@svgr/webpack?-prettier,-svgo![path]"
]
],
"cacheDirectory": true,
"cacheCompression": false
,
"test": ,
"exclude": ,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-loader\lib\index.js",
"options":
"babelrc": false,
"configFile": false,
"compact": false,
"presets": [
[
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\babel-preset-react-app\dependencies.js",
"helpers": true
]
],
"cacheDirectory": true,
"cacheCompression": false,
"cacheIdentifier": "development:babel-plugin-named-asset-import@0.2.3:babel-preset-react-app@6.1.0:react-dev-utils@6.1.1:react-scripts@2.1.1",
"sourceMaps": false
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 1,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
]
,
"test": ,
"exclude": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"test": ,
"use": [
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\style-loader\index.js",
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\css-loader\index.js",
"options":
"importLoaders": 2,
"modules": true
,
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\postcss-loader\src\index.js",
"options":
"ident": "postcss"
,
"C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\sass-loader\lib\loader.js"
]
,
"exclude": [
,
,
],
"loader": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules\file-loader\dist\cjs.js",
"options":
"name": "static/media/[name].[hash:8].[ext]"
]
]
,
"plugins": [
"options":
"template": "C:\Users\me\Documents\nodejs\MY_PROJECT\public\index.html",
"templateContent": false,
"filename": "index.html",
"hash": false,
"inject": true,
"compile": true,
"favicon": false,
"minify": false,
"cache": true,
"showErrors": true,
"chunks": "all",
"excludeChunks": [],
"chunksSortMode": "auto",
"meta": ,
"title": "Webpack App",
"xhtml": false
,
"version": 4
,
"replacements":
"NODE_ENV": "development",
"PUBLIC_URL": "",
"REACT_APP_DEFAULT_LANGUAGE": "fr"
,
"appPath": "C:\Users\me\Documents\nodejs\MY_PROJECT"
,
"definitions":
"process.env":
"NODE_ENV": ""development"",
"PUBLIC_URL": """",
"REACT_APP_DEFAULT_LANGUAGE": ""fr""
,
"options": ,
"fullBuildTimeout": 200,
"requestTimeout": 10000
,
"options": ,
"pathCache": ,
"fsOperations": 0,
"primed": false
,
"nodeModulesPath": "C:\Users\me\Documents\nodejs\MY_PROJECT\node_modules"
,
"options":
"resourceRegExp": ,
"contextRegExp":
,
"opts":
"publicPath": "/",
"basePath": "",
"fileName": "asset-manifest.json",
"transformExtensions": ,
"writeToFileEmit": false,
"seed": null,
"filter": null,
"map": null,
"generate": null,
"sort": null
],
"node":
"dgram": "empty",
"fs": "empty",
"net": "empty",
"tls": "empty",
"child_process": "empty"
,
"performance": false
I log it before calling the delete
operation and as we can see their is no ModuleScopePlugin
. So why does it still failing?
Any help is welcome.
Thanks.
reactjs model-view-controller
reactjs model-view-controller
edited Mar 8 at 15:07
MHogge
asked Feb 27 at 12:26
MHoggeMHogge
1,35342859
1,35342859
stackoverflow.com/questions/44114436/… . And use CommonJS exports instead of ES modules.
– estus
Feb 27 at 13:07
But it require to do aneject
. What if I do not want to eject my app?
– MHogge
Feb 27 at 13:35
1
You may use react-app-rewired. It's either one or another. A more generalized way is to make privatecommon
package and use it in both.
– estus
Feb 27 at 13:44
I didn't know about react-app-rewired (I'm new to react). I'll try it soon, thanks.
– MHogge
Feb 27 at 13:47
I triedreact-app-rewired
but it doesn't seem to be working. Any idea of what I'm doing wrong?
– MHogge
Mar 8 at 14:47
add a comment |
stackoverflow.com/questions/44114436/… . And use CommonJS exports instead of ES modules.
– estus
Feb 27 at 13:07
But it require to do aneject
. What if I do not want to eject my app?
– MHogge
Feb 27 at 13:35
1
You may use react-app-rewired. It's either one or another. A more generalized way is to make privatecommon
package and use it in both.
– estus
Feb 27 at 13:44
I didn't know about react-app-rewired (I'm new to react). I'll try it soon, thanks.
– MHogge
Feb 27 at 13:47
I triedreact-app-rewired
but it doesn't seem to be working. Any idea of what I'm doing wrong?
– MHogge
Mar 8 at 14:47
stackoverflow.com/questions/44114436/… . And use CommonJS exports instead of ES modules.
– estus
Feb 27 at 13:07
stackoverflow.com/questions/44114436/… . And use CommonJS exports instead of ES modules.
– estus
Feb 27 at 13:07
But it require to do an
eject
. What if I do not want to eject my app?– MHogge
Feb 27 at 13:35
But it require to do an
eject
. What if I do not want to eject my app?– MHogge
Feb 27 at 13:35
1
1
You may use react-app-rewired. It's either one or another. A more generalized way is to make private
common
package and use it in both.– estus
Feb 27 at 13:44
You may use react-app-rewired. It's either one or another. A more generalized way is to make private
common
package and use it in both.– estus
Feb 27 at 13:44
I didn't know about react-app-rewired (I'm new to react). I'll try it soon, thanks.
– MHogge
Feb 27 at 13:47
I didn't know about react-app-rewired (I'm new to react). I'll try it soon, thanks.
– MHogge
Feb 27 at 13:47
I tried
react-app-rewired
but it doesn't seem to be working. Any idea of what I'm doing wrong?– MHogge
Mar 8 at 14:47
I tried
react-app-rewired
but it doesn't seem to be working. Any idea of what I'm doing wrong?– MHogge
Mar 8 at 14:47
add a comment |
1 Answer
1
active
oldest
votes
Sharing code is what modules are for. Ideally you would package up you models into a module and install/import into each codebase. To keep development simple you would probably want to utilise a monorepo format using Lerna and Yarn (or similar alternatives).
If that's beyond the scope of your project then I think you should be able to get away with using CommonJS exports in your model code (as previously suggested) which you keep in the React app src and require from your server code.
./src/models/DataModel.js
:
class DataModel
constructor(name)
this.name = name;
module.exports = DataModel ;
from React app
import DataModel from "./models/DataModel";
from server
const DataModel = require("./src/models/DataModel");
=========================================================================
Update
So as far as I can tell we're hitting some deep js-module voodoo here.
The code I suggested above works in codesandbox, but not locally.
I have tried matching the react-scripts and react versions with those in codesandbox with no success. I can only assume that codesandbox has it's own coping mechanisms which alleviate the problem.
As soon as the class is defined in the module using the commonjs export, things get weird with
module.exports = DataModel ;
and import DataModel
give Attempted import error: 'DataModel' is not exported from '../models/DataModel'.
module.exports = DataModel ;
and require
gives TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
and
exports.DataModel = DataModel;
and require
gives ReferenceError: exports is not defined
Swap out the class
for an old-school "class" though, and require
the module from our app, and everything works as expected.
function DataModel(name)
this.name = name;
DataModel.prototype.logName = function ()
console.log(this.name);
module.exports = DataModel ;
It seems it's to do with all the magic being performed by webpack/babel/et al to deal with the different module formats floating around, but I really couldn't nail it down.
For a second I thought this was going to explain it, but.. it didn't.. https://medium.com/webpack/webpack-4-import-and-commonjs-d619d626b655
In fact, sharing the code is probably the best solution in most cases but is not what we're going to do for internal reasons (+ the project is small and do not need to be split). The solution with mixingimport
andrequire
is ok but I still have an issue about this: I can't use my constructor inside the React app. If I trynew DataModel('my model');
I got the following error: Attempted import error: 'DataModel' is not exported from './models/DataModel'.
– MHogge
Mar 11 at 14:11
did you use the named import format, ieimport DataModel
rather than the default import format,import DataModel
?
– lecstor
Mar 11 at 21:19
I used the named import format (like you did in the example)
– MHogge
Mar 11 at 22:40
I'm not sure what's going wrong then. Maybe something in your webpack config? I don't see why that wouldn't work, and it works for me here.. codesandbox.io/s/8l17wn84x0
– lecstor
Mar 11 at 22:52
My webpack config is the default one given by create-react-app. But anyway, thank you for your time, I will continue to search a solution for this.
– MHogge
Mar 12 at 8:18
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54905481%2fshare-model-between-backend-and-frontend-when-using-create-react-app%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sharing code is what modules are for. Ideally you would package up you models into a module and install/import into each codebase. To keep development simple you would probably want to utilise a monorepo format using Lerna and Yarn (or similar alternatives).
If that's beyond the scope of your project then I think you should be able to get away with using CommonJS exports in your model code (as previously suggested) which you keep in the React app src and require from your server code.
./src/models/DataModel.js
:
class DataModel
constructor(name)
this.name = name;
module.exports = DataModel ;
from React app
import DataModel from "./models/DataModel";
from server
const DataModel = require("./src/models/DataModel");
=========================================================================
Update
So as far as I can tell we're hitting some deep js-module voodoo here.
The code I suggested above works in codesandbox, but not locally.
I have tried matching the react-scripts and react versions with those in codesandbox with no success. I can only assume that codesandbox has it's own coping mechanisms which alleviate the problem.
As soon as the class is defined in the module using the commonjs export, things get weird with
module.exports = DataModel ;
and import DataModel
give Attempted import error: 'DataModel' is not exported from '../models/DataModel'.
module.exports = DataModel ;
and require
gives TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
and
exports.DataModel = DataModel;
and require
gives ReferenceError: exports is not defined
Swap out the class
for an old-school "class" though, and require
the module from our app, and everything works as expected.
function DataModel(name)
this.name = name;
DataModel.prototype.logName = function ()
console.log(this.name);
module.exports = DataModel ;
It seems it's to do with all the magic being performed by webpack/babel/et al to deal with the different module formats floating around, but I really couldn't nail it down.
For a second I thought this was going to explain it, but.. it didn't.. https://medium.com/webpack/webpack-4-import-and-commonjs-d619d626b655
In fact, sharing the code is probably the best solution in most cases but is not what we're going to do for internal reasons (+ the project is small and do not need to be split). The solution with mixingimport
andrequire
is ok but I still have an issue about this: I can't use my constructor inside the React app. If I trynew DataModel('my model');
I got the following error: Attempted import error: 'DataModel' is not exported from './models/DataModel'.
– MHogge
Mar 11 at 14:11
did you use the named import format, ieimport DataModel
rather than the default import format,import DataModel
?
– lecstor
Mar 11 at 21:19
I used the named import format (like you did in the example)
– MHogge
Mar 11 at 22:40
I'm not sure what's going wrong then. Maybe something in your webpack config? I don't see why that wouldn't work, and it works for me here.. codesandbox.io/s/8l17wn84x0
– lecstor
Mar 11 at 22:52
My webpack config is the default one given by create-react-app. But anyway, thank you for your time, I will continue to search a solution for this.
– MHogge
Mar 12 at 8:18
|
show 3 more comments
Sharing code is what modules are for. Ideally you would package up you models into a module and install/import into each codebase. To keep development simple you would probably want to utilise a monorepo format using Lerna and Yarn (or similar alternatives).
If that's beyond the scope of your project then I think you should be able to get away with using CommonJS exports in your model code (as previously suggested) which you keep in the React app src and require from your server code.
./src/models/DataModel.js
:
class DataModel
constructor(name)
this.name = name;
module.exports = DataModel ;
from React app
import DataModel from "./models/DataModel";
from server
const DataModel = require("./src/models/DataModel");
=========================================================================
Update
So as far as I can tell we're hitting some deep js-module voodoo here.
The code I suggested above works in codesandbox, but not locally.
I have tried matching the react-scripts and react versions with those in codesandbox with no success. I can only assume that codesandbox has it's own coping mechanisms which alleviate the problem.
As soon as the class is defined in the module using the commonjs export, things get weird with
module.exports = DataModel ;
and import DataModel
give Attempted import error: 'DataModel' is not exported from '../models/DataModel'.
module.exports = DataModel ;
and require
gives TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
and
exports.DataModel = DataModel;
and require
gives ReferenceError: exports is not defined
Swap out the class
for an old-school "class" though, and require
the module from our app, and everything works as expected.
function DataModel(name)
this.name = name;
DataModel.prototype.logName = function ()
console.log(this.name);
module.exports = DataModel ;
It seems it's to do with all the magic being performed by webpack/babel/et al to deal with the different module formats floating around, but I really couldn't nail it down.
For a second I thought this was going to explain it, but.. it didn't.. https://medium.com/webpack/webpack-4-import-and-commonjs-d619d626b655
In fact, sharing the code is probably the best solution in most cases but is not what we're going to do for internal reasons (+ the project is small and do not need to be split). The solution with mixingimport
andrequire
is ok but I still have an issue about this: I can't use my constructor inside the React app. If I trynew DataModel('my model');
I got the following error: Attempted import error: 'DataModel' is not exported from './models/DataModel'.
– MHogge
Mar 11 at 14:11
did you use the named import format, ieimport DataModel
rather than the default import format,import DataModel
?
– lecstor
Mar 11 at 21:19
I used the named import format (like you did in the example)
– MHogge
Mar 11 at 22:40
I'm not sure what's going wrong then. Maybe something in your webpack config? I don't see why that wouldn't work, and it works for me here.. codesandbox.io/s/8l17wn84x0
– lecstor
Mar 11 at 22:52
My webpack config is the default one given by create-react-app. But anyway, thank you for your time, I will continue to search a solution for this.
– MHogge
Mar 12 at 8:18
|
show 3 more comments
Sharing code is what modules are for. Ideally you would package up you models into a module and install/import into each codebase. To keep development simple you would probably want to utilise a monorepo format using Lerna and Yarn (or similar alternatives).
If that's beyond the scope of your project then I think you should be able to get away with using CommonJS exports in your model code (as previously suggested) which you keep in the React app src and require from your server code.
./src/models/DataModel.js
:
class DataModel
constructor(name)
this.name = name;
module.exports = DataModel ;
from React app
import DataModel from "./models/DataModel";
from server
const DataModel = require("./src/models/DataModel");
=========================================================================
Update
So as far as I can tell we're hitting some deep js-module voodoo here.
The code I suggested above works in codesandbox, but not locally.
I have tried matching the react-scripts and react versions with those in codesandbox with no success. I can only assume that codesandbox has it's own coping mechanisms which alleviate the problem.
As soon as the class is defined in the module using the commonjs export, things get weird with
module.exports = DataModel ;
and import DataModel
give Attempted import error: 'DataModel' is not exported from '../models/DataModel'.
module.exports = DataModel ;
and require
gives TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
and
exports.DataModel = DataModel;
and require
gives ReferenceError: exports is not defined
Swap out the class
for an old-school "class" though, and require
the module from our app, and everything works as expected.
function DataModel(name)
this.name = name;
DataModel.prototype.logName = function ()
console.log(this.name);
module.exports = DataModel ;
It seems it's to do with all the magic being performed by webpack/babel/et al to deal with the different module formats floating around, but I really couldn't nail it down.
For a second I thought this was going to explain it, but.. it didn't.. https://medium.com/webpack/webpack-4-import-and-commonjs-d619d626b655
Sharing code is what modules are for. Ideally you would package up you models into a module and install/import into each codebase. To keep development simple you would probably want to utilise a monorepo format using Lerna and Yarn (or similar alternatives).
If that's beyond the scope of your project then I think you should be able to get away with using CommonJS exports in your model code (as previously suggested) which you keep in the React app src and require from your server code.
./src/models/DataModel.js
:
class DataModel
constructor(name)
this.name = name;
module.exports = DataModel ;
from React app
import DataModel from "./models/DataModel";
from server
const DataModel = require("./src/models/DataModel");
=========================================================================
Update
So as far as I can tell we're hitting some deep js-module voodoo here.
The code I suggested above works in codesandbox, but not locally.
I have tried matching the react-scripts and react versions with those in codesandbox with no success. I can only assume that codesandbox has it's own coping mechanisms which alleviate the problem.
As soon as the class is defined in the module using the commonjs export, things get weird with
module.exports = DataModel ;
and import DataModel
give Attempted import error: 'DataModel' is not exported from '../models/DataModel'.
module.exports = DataModel ;
and require
gives TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
and
exports.DataModel = DataModel;
and require
gives ReferenceError: exports is not defined
Swap out the class
for an old-school "class" though, and require
the module from our app, and everything works as expected.
function DataModel(name)
this.name = name;
DataModel.prototype.logName = function ()
console.log(this.name);
module.exports = DataModel ;
It seems it's to do with all the magic being performed by webpack/babel/et al to deal with the different module formats floating around, but I really couldn't nail it down.
For a second I thought this was going to explain it, but.. it didn't.. https://medium.com/webpack/webpack-4-import-and-commonjs-d619d626b655
edited Mar 12 at 13:12
answered Mar 9 at 4:33
lecstorlecstor
4,0861318
4,0861318
In fact, sharing the code is probably the best solution in most cases but is not what we're going to do for internal reasons (+ the project is small and do not need to be split). The solution with mixingimport
andrequire
is ok but I still have an issue about this: I can't use my constructor inside the React app. If I trynew DataModel('my model');
I got the following error: Attempted import error: 'DataModel' is not exported from './models/DataModel'.
– MHogge
Mar 11 at 14:11
did you use the named import format, ieimport DataModel
rather than the default import format,import DataModel
?
– lecstor
Mar 11 at 21:19
I used the named import format (like you did in the example)
– MHogge
Mar 11 at 22:40
I'm not sure what's going wrong then. Maybe something in your webpack config? I don't see why that wouldn't work, and it works for me here.. codesandbox.io/s/8l17wn84x0
– lecstor
Mar 11 at 22:52
My webpack config is the default one given by create-react-app. But anyway, thank you for your time, I will continue to search a solution for this.
– MHogge
Mar 12 at 8:18
|
show 3 more comments
In fact, sharing the code is probably the best solution in most cases but is not what we're going to do for internal reasons (+ the project is small and do not need to be split). The solution with mixingimport
andrequire
is ok but I still have an issue about this: I can't use my constructor inside the React app. If I trynew DataModel('my model');
I got the following error: Attempted import error: 'DataModel' is not exported from './models/DataModel'.
– MHogge
Mar 11 at 14:11
did you use the named import format, ieimport DataModel
rather than the default import format,import DataModel
?
– lecstor
Mar 11 at 21:19
I used the named import format (like you did in the example)
– MHogge
Mar 11 at 22:40
I'm not sure what's going wrong then. Maybe something in your webpack config? I don't see why that wouldn't work, and it works for me here.. codesandbox.io/s/8l17wn84x0
– lecstor
Mar 11 at 22:52
My webpack config is the default one given by create-react-app. But anyway, thank you for your time, I will continue to search a solution for this.
– MHogge
Mar 12 at 8:18
In fact, sharing the code is probably the best solution in most cases but is not what we're going to do for internal reasons (+ the project is small and do not need to be split). The solution with mixing
import
and require
is ok but I still have an issue about this: I can't use my constructor inside the React app. If I try new DataModel('my model');
I got the following error: Attempted import error: 'DataModel' is not exported from './models/DataModel'.– MHogge
Mar 11 at 14:11
In fact, sharing the code is probably the best solution in most cases but is not what we're going to do for internal reasons (+ the project is small and do not need to be split). The solution with mixing
import
and require
is ok but I still have an issue about this: I can't use my constructor inside the React app. If I try new DataModel('my model');
I got the following error: Attempted import error: 'DataModel' is not exported from './models/DataModel'.– MHogge
Mar 11 at 14:11
did you use the named import format, ie
import DataModel
rather than the default import format, import DataModel
?– lecstor
Mar 11 at 21:19
did you use the named import format, ie
import DataModel
rather than the default import format, import DataModel
?– lecstor
Mar 11 at 21:19
I used the named import format (like you did in the example)
– MHogge
Mar 11 at 22:40
I used the named import format (like you did in the example)
– MHogge
Mar 11 at 22:40
I'm not sure what's going wrong then. Maybe something in your webpack config? I don't see why that wouldn't work, and it works for me here.. codesandbox.io/s/8l17wn84x0
– lecstor
Mar 11 at 22:52
I'm not sure what's going wrong then. Maybe something in your webpack config? I don't see why that wouldn't work, and it works for me here.. codesandbox.io/s/8l17wn84x0
– lecstor
Mar 11 at 22:52
My webpack config is the default one given by create-react-app. But anyway, thank you for your time, I will continue to search a solution for this.
– MHogge
Mar 12 at 8:18
My webpack config is the default one given by create-react-app. But anyway, thank you for your time, I will continue to search a solution for this.
– MHogge
Mar 12 at 8:18
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54905481%2fshare-model-between-backend-and-frontend-when-using-create-react-app%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/questions/44114436/… . And use CommonJS exports instead of ES modules.
– estus
Feb 27 at 13:07
But it require to do an
eject
. What if I do not want to eject my app?– MHogge
Feb 27 at 13:35
1
You may use react-app-rewired. It's either one or another. A more generalized way is to make private
common
package and use it in both.– estus
Feb 27 at 13:44
I didn't know about react-app-rewired (I'm new to react). I'll try it soon, thanks.
– MHogge
Feb 27 at 13:47
I tried
react-app-rewired
but it doesn't seem to be working. Any idea of what I'm doing wrong?– MHogge
Mar 8 at 14:47