How can I allow relative paths to be set in my css with React Webpack?
I'm fairly new to Webpack so I hope this question is understandable, I've tried to format it to be easy to read.
I'm using webpack to build my react app and I have finally figured out how to output my image folder structure and have hit another road block.
I hope you can help me.
Outputted dist folder structure below:
I'm not sure why but in development I have to reference background-image url paths in a specific way. Issue is after running webpack, my outputted distribution build doesn't seem to like that the image file paths aren't relative.
In development my css is set as so:background: url('/img/site/placeholder.jpg');
The build works fine in development but in production the image file is not found:GET file:///img/site/placeholder.jpg net::ERR_FILE_NOT_FOUND
I can update the minified bundle.js so the css uses relative paths like so:background: url('./img/site/placeholder.jpg');
This works only in production and not in development.
If I run webpack to output my React build for distribution with the relative path(./img/) in the css, I get this error:ERROR in ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss
Module not found: Error: Can't resolve './img/site/placeholder.jpg' in '/Users/moehammo/Documents/_Moe/00_MHC/conceptual_website_react/scss'
@ ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss 6:6248-6285
@ ./scss/styles.scss
@ ./index.js
I've tried using publicPath: "./" in my webpack.config.js file but it throws my build out and I think I might be implementing it incorrectly. What am I missing in this conundrum?
My webpack.config.js file below:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const sass = require('node-sass');
const path = require('path');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin(
template: './index.html',
filename: 'index.html',
inject: 'body'
)
module.exports =
entry: './index.js',
output:
path: path.resolve(__dirname, "dist"),
filename: 'bundle.js'
,
module:
rules: [
test:/.js$/,
exclude: /(node_modules)/,
use:
loader: 'babel-loader',
options:
presets:['env','react']
,
test: /.scss$/,
use: [
loader: "style-loader"
,
loader: "css-loader"
,
loader: "sass-loader"
]
,
test: /.(png
],
loaders: [
test: /.json$/, loader: 'json'
// other loaders
]
,
plugins: [HtmlWebpackPluginConfig]
I appreciate any help.
Thank you
reactjs webpack
add a comment |
I'm fairly new to Webpack so I hope this question is understandable, I've tried to format it to be easy to read.
I'm using webpack to build my react app and I have finally figured out how to output my image folder structure and have hit another road block.
I hope you can help me.
Outputted dist folder structure below:
I'm not sure why but in development I have to reference background-image url paths in a specific way. Issue is after running webpack, my outputted distribution build doesn't seem to like that the image file paths aren't relative.
In development my css is set as so:background: url('/img/site/placeholder.jpg');
The build works fine in development but in production the image file is not found:GET file:///img/site/placeholder.jpg net::ERR_FILE_NOT_FOUND
I can update the minified bundle.js so the css uses relative paths like so:background: url('./img/site/placeholder.jpg');
This works only in production and not in development.
If I run webpack to output my React build for distribution with the relative path(./img/) in the css, I get this error:ERROR in ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss
Module not found: Error: Can't resolve './img/site/placeholder.jpg' in '/Users/moehammo/Documents/_Moe/00_MHC/conceptual_website_react/scss'
@ ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss 6:6248-6285
@ ./scss/styles.scss
@ ./index.js
I've tried using publicPath: "./" in my webpack.config.js file but it throws my build out and I think I might be implementing it incorrectly. What am I missing in this conundrum?
My webpack.config.js file below:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const sass = require('node-sass');
const path = require('path');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin(
template: './index.html',
filename: 'index.html',
inject: 'body'
)
module.exports =
entry: './index.js',
output:
path: path.resolve(__dirname, "dist"),
filename: 'bundle.js'
,
module:
rules: [
test:/.js$/,
exclude: /(node_modules)/,
use:
loader: 'babel-loader',
options:
presets:['env','react']
,
test: /.scss$/,
use: [
loader: "style-loader"
,
loader: "css-loader"
,
loader: "sass-loader"
]
,
test: /.(png
],
loaders: [
test: /.json$/, loader: 'json'
// other loaders
]
,
plugins: [HtmlWebpackPluginConfig]
I appreciate any help.
Thank you
reactjs webpack
Are u usingwebpack-dev-serverfor development?
– Xlee
Sep 7 '17 at 6:43
Yes, I am usingwebpack-dev-server.
– Moe-Joe
Sep 7 '17 at 21:30
My suggestion is to change the relative url from./img/site/placeholder.jpg->../img/site/placeholder.jpg, i dont even think your file loader can work based on your folder structure.
– Xlee
Sep 8 '17 at 4:05
add a comment |
I'm fairly new to Webpack so I hope this question is understandable, I've tried to format it to be easy to read.
I'm using webpack to build my react app and I have finally figured out how to output my image folder structure and have hit another road block.
I hope you can help me.
Outputted dist folder structure below:
I'm not sure why but in development I have to reference background-image url paths in a specific way. Issue is after running webpack, my outputted distribution build doesn't seem to like that the image file paths aren't relative.
In development my css is set as so:background: url('/img/site/placeholder.jpg');
The build works fine in development but in production the image file is not found:GET file:///img/site/placeholder.jpg net::ERR_FILE_NOT_FOUND
I can update the minified bundle.js so the css uses relative paths like so:background: url('./img/site/placeholder.jpg');
This works only in production and not in development.
If I run webpack to output my React build for distribution with the relative path(./img/) in the css, I get this error:ERROR in ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss
Module not found: Error: Can't resolve './img/site/placeholder.jpg' in '/Users/moehammo/Documents/_Moe/00_MHC/conceptual_website_react/scss'
@ ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss 6:6248-6285
@ ./scss/styles.scss
@ ./index.js
I've tried using publicPath: "./" in my webpack.config.js file but it throws my build out and I think I might be implementing it incorrectly. What am I missing in this conundrum?
My webpack.config.js file below:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const sass = require('node-sass');
const path = require('path');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin(
template: './index.html',
filename: 'index.html',
inject: 'body'
)
module.exports =
entry: './index.js',
output:
path: path.resolve(__dirname, "dist"),
filename: 'bundle.js'
,
module:
rules: [
test:/.js$/,
exclude: /(node_modules)/,
use:
loader: 'babel-loader',
options:
presets:['env','react']
,
test: /.scss$/,
use: [
loader: "style-loader"
,
loader: "css-loader"
,
loader: "sass-loader"
]
,
test: /.(png
],
loaders: [
test: /.json$/, loader: 'json'
// other loaders
]
,
plugins: [HtmlWebpackPluginConfig]
I appreciate any help.
Thank you
reactjs webpack
I'm fairly new to Webpack so I hope this question is understandable, I've tried to format it to be easy to read.
I'm using webpack to build my react app and I have finally figured out how to output my image folder structure and have hit another road block.
I hope you can help me.
Outputted dist folder structure below:
I'm not sure why but in development I have to reference background-image url paths in a specific way. Issue is after running webpack, my outputted distribution build doesn't seem to like that the image file paths aren't relative.
In development my css is set as so:background: url('/img/site/placeholder.jpg');
The build works fine in development but in production the image file is not found:GET file:///img/site/placeholder.jpg net::ERR_FILE_NOT_FOUND
I can update the minified bundle.js so the css uses relative paths like so:background: url('./img/site/placeholder.jpg');
This works only in production and not in development.
If I run webpack to output my React build for distribution with the relative path(./img/) in the css, I get this error:ERROR in ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss
Module not found: Error: Can't resolve './img/site/placeholder.jpg' in '/Users/moehammo/Documents/_Moe/00_MHC/conceptual_website_react/scss'
@ ./node_modules/css-loader!./node_modules/sass-loader/lib/loader.js!./scss/styles.scss 6:6248-6285
@ ./scss/styles.scss
@ ./index.js
I've tried using publicPath: "./" in my webpack.config.js file but it throws my build out and I think I might be implementing it incorrectly. What am I missing in this conundrum?
My webpack.config.js file below:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const sass = require('node-sass');
const path = require('path');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin(
template: './index.html',
filename: 'index.html',
inject: 'body'
)
module.exports =
entry: './index.js',
output:
path: path.resolve(__dirname, "dist"),
filename: 'bundle.js'
,
module:
rules: [
test:/.js$/,
exclude: /(node_modules)/,
use:
loader: 'babel-loader',
options:
presets:['env','react']
,
test: /.scss$/,
use: [
loader: "style-loader"
,
loader: "css-loader"
,
loader: "sass-loader"
]
,
test: /.(png
],
loaders: [
test: /.json$/, loader: 'json'
// other loaders
]
,
plugins: [HtmlWebpackPluginConfig]
I appreciate any help.
Thank you
reactjs webpack
reactjs webpack
asked Sep 7 '17 at 5:46
Moe-JoeMoe-Joe
4411522
4411522
Are u usingwebpack-dev-serverfor development?
– Xlee
Sep 7 '17 at 6:43
Yes, I am usingwebpack-dev-server.
– Moe-Joe
Sep 7 '17 at 21:30
My suggestion is to change the relative url from./img/site/placeholder.jpg->../img/site/placeholder.jpg, i dont even think your file loader can work based on your folder structure.
– Xlee
Sep 8 '17 at 4:05
add a comment |
Are u usingwebpack-dev-serverfor development?
– Xlee
Sep 7 '17 at 6:43
Yes, I am usingwebpack-dev-server.
– Moe-Joe
Sep 7 '17 at 21:30
My suggestion is to change the relative url from./img/site/placeholder.jpg->../img/site/placeholder.jpg, i dont even think your file loader can work based on your folder structure.
– Xlee
Sep 8 '17 at 4:05
Are u using
webpack-dev-server for development?– Xlee
Sep 7 '17 at 6:43
Are u using
webpack-dev-server for development?– Xlee
Sep 7 '17 at 6:43
Yes, I am using
webpack-dev-server.– Moe-Joe
Sep 7 '17 at 21:30
Yes, I am using
webpack-dev-server.– Moe-Joe
Sep 7 '17 at 21:30
My suggestion is to change the relative url from
./img/site/placeholder.jpg -> ../img/site/placeholder.jpg, i dont even think your file loader can work based on your folder structure.– Xlee
Sep 8 '17 at 4:05
My suggestion is to change the relative url from
./img/site/placeholder.jpg -> ../img/site/placeholder.jpg, i dont even think your file loader can work based on your folder structure.– Xlee
Sep 8 '17 at 4:05
add a comment |
1 Answer
1
active
oldest
votes
One possibility would be using url-loader which is going to convert the file into a base64 string which will be applied directly into your image file when needed.
// regular js file
import img from './image.png'
console.log(img);
// webpack.config.js
module.exports =
module:
rules: [
gif)$/i,
use: [
loader: 'url-loader',
options:
limit: 8192
]
]
If you want to continue with file-loader try working with the outputPath property on the loader config in order to match the location of the production build folder. It's likely your production app is trying to access a file that doesn't exist in your "dist" folder.
loader: 'file-loader',
options:
name: '[path][name].[ext]',
outputPath: 'images/'
add a comment |
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%2f46088685%2fhow-can-i-allow-relative-paths-to-be-set-in-my-css-with-react-webpack%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
One possibility would be using url-loader which is going to convert the file into a base64 string which will be applied directly into your image file when needed.
// regular js file
import img from './image.png'
console.log(img);
// webpack.config.js
module.exports =
module:
rules: [
gif)$/i,
use: [
loader: 'url-loader',
options:
limit: 8192
]
]
If you want to continue with file-loader try working with the outputPath property on the loader config in order to match the location of the production build folder. It's likely your production app is trying to access a file that doesn't exist in your "dist" folder.
loader: 'file-loader',
options:
name: '[path][name].[ext]',
outputPath: 'images/'
add a comment |
One possibility would be using url-loader which is going to convert the file into a base64 string which will be applied directly into your image file when needed.
// regular js file
import img from './image.png'
console.log(img);
// webpack.config.js
module.exports =
module:
rules: [
gif)$/i,
use: [
loader: 'url-loader',
options:
limit: 8192
]
]
If you want to continue with file-loader try working with the outputPath property on the loader config in order to match the location of the production build folder. It's likely your production app is trying to access a file that doesn't exist in your "dist" folder.
loader: 'file-loader',
options:
name: '[path][name].[ext]',
outputPath: 'images/'
add a comment |
One possibility would be using url-loader which is going to convert the file into a base64 string which will be applied directly into your image file when needed.
// regular js file
import img from './image.png'
console.log(img);
// webpack.config.js
module.exports =
module:
rules: [
gif)$/i,
use: [
loader: 'url-loader',
options:
limit: 8192
]
]
If you want to continue with file-loader try working with the outputPath property on the loader config in order to match the location of the production build folder. It's likely your production app is trying to access a file that doesn't exist in your "dist" folder.
loader: 'file-loader',
options:
name: '[path][name].[ext]',
outputPath: 'images/'
One possibility would be using url-loader which is going to convert the file into a base64 string which will be applied directly into your image file when needed.
// regular js file
import img from './image.png'
console.log(img);
// webpack.config.js
module.exports =
module:
rules: [
gif)$/i,
use: [
loader: 'url-loader',
options:
limit: 8192
]
]
If you want to continue with file-loader try working with the outputPath property on the loader config in order to match the location of the production build folder. It's likely your production app is trying to access a file that doesn't exist in your "dist" folder.
loader: 'file-loader',
options:
name: '[path][name].[ext]',
outputPath: 'images/'
answered Nov 14 '18 at 1:48
zavjszavjs
1697
1697
add a comment |
add a comment |
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%2f46088685%2fhow-can-i-allow-relative-paths-to-be-set-in-my-css-with-react-webpack%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
Are u using
webpack-dev-serverfor development?– Xlee
Sep 7 '17 at 6:43
Yes, I am using
webpack-dev-server.– Moe-Joe
Sep 7 '17 at 21:30
My suggestion is to change the relative url from
./img/site/placeholder.jpg->../img/site/placeholder.jpg, i dont even think your file loader can work based on your folder structure.– Xlee
Sep 8 '17 at 4:05