Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Cannot resolve module 'json' #17

Closed
es6Test opened this issue Mar 17, 2016 · 30 comments
Closed

Cannot resolve module 'json' #17

es6Test opened this issue Mar 17, 2016 · 30 comments
Assignees

Comments

@es6Test
Copy link

es6Test commented Mar 17, 2016

Hi, I am trying this, I am confident my paths are correct

var Page = require('json!./json/foo.json');

and get this error

ERROR in ../mocks/mockApi.js
Module not found: Error: Cannot resolve module 'json' in /Users/me/ui/src/components/native/mocks
 @ ../mocks/mockApi.js 1:31-83
@es6Test
Copy link
Author

es6Test commented Mar 17, 2016

Webpack:

  {test: /\.json$/, loader: 'json-loader'}

@cch5ng
Copy link

cch5ng commented Mar 19, 2016

@es6Test specifying the loader as 'json' (without the '-loader') worked for me.

{test: /\.json$/, loader: "json"}

@es6Test
Copy link
Author

es6Test commented Mar 19, 2016

thanks but still no luck:

module.exports = {
  context: __dirname,
  entry: {
    main: [
      "./index"
    ]
  },
  output: {
    path: __dirname + "/dist",
    filename: "components.js"
  },
  devtool: "inline-source-map",
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015']
        },
        include: [__dirname + '../', __dirname],
        exclude: [__dirname + '/node_modules', __dirname + '/dist']

      },
      {test: /\.json$/, loader: "json"}


    ]
  },
  resolve: {
    // you can now require('file') instead of require('file.js')
    extensions: ['', '.js', '.json']
  }
}

@es6Test
Copy link
Author

es6Test commented Mar 19, 2016

{
  "name": "__Demo__",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack --watch --watch-polling"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.4.5",
    "babel-loader": "^6.2.1",
    "babel-preset-es2015": "^6.3.13",
    "css-loader": "^0.23.1",
    "json-loader": "^0.5.4",
    "style-loader": "^0.13.0",
    "stylus-loader": "^1.5.1",
    "webpack": "^1.12.12"
  }
}

@isaac-peka
Copy link

I'm experiencing the same problem, path is definitely correct

@xtuc
Copy link

xtuc commented May 30, 2016

I solved the problem by adding an include key in the loader object.

{test: /\.json$/, loader: "json", include: "path/to/your/sources"}

@ashelley
Copy link

ashelley commented Aug 5, 2016

weirdly... I was just having this issue and after running an npm install --save again it started resolving shrug

@jandob
Copy link

jandob commented Aug 22, 2016

Had the same problem but then recognized that the json-loader was not installed (and not in package.json). Shouldn't webpack complain in this case? Seems like a bug in webpack.

@tianleiyiji123
Copy link

it worked when i use the "var Page = require('json!./json/foo.json');",but not worked in "webpack.config.js"

@kxxoling
Copy link

kxxoling commented Sep 1, 2016

Same issue. Fixed by adding include in webpack.config.js and using var foo = require('json!./path/to/foo.json') instead of var foo = require('./path/to/foo').

@xiangzhuyuan
Copy link

I think the most case of this issue is just because you didn't add the dev dependency for css-loader. since most of docs about css-loader just mention how to use that in js file.

@kevinSuttle
Copy link

kevinSuttle commented Oct 4, 2016

This is still an issue as of Webpack 2.1.0-beta25. Using that json! syntax has never once worked with require() for me, no matter which path I give it. Using json-loader either breaks, or fails silently.

screen shot 2016-10-04 at 4 10 04 pm

@kevinSuttle kevinSuttle mentioned this issue Oct 4, 2016
@JWplanet
Copy link

JWplanet commented Oct 6, 2016

I could solve similiar error(not in mock) with npm install json-loader --save. I didn't install it.

@niedomnie
Copy link

niedomnie commented Nov 10, 2016

I've got the same problem - json load directly works, by webpack does not.

works: import config from "!json!../../cfg/config.json" (I need to have !json! istead of json!)
does not: import config from "../../cfg/config.json and either in webpack.config { loader: 'json-loader', test: /.json$/ } or { loader: 'json', test: /.json$/ } or with include option - all does not work

versions:
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"json-loader": "^0.5.4",
"webpack": "^1.13.3"

@Kerita
Copy link

Kerita commented Nov 22, 2016

I have absolutely installed all packages,but I also met this problem.When I do 'npm install json-loader --save' like JWplanet above ,it worked.I hoped that it would help you.

@fellipejsousa
Copy link

fellipejsousa commented Jan 5, 2017

In webpack:
{ test: /\.json$/, include: helpers.root('public', 'data'), loader: 'raw' },

In main.js:
json = require("json-loader!./conteudo.json"),

in mains.js example:
carregaIdiomaPadrao: function () { $.i18n().load(json); if(minhasessionlang === null){ carregaIdiomas.alteraIdioma('br'); }else{ carregaIdiomas.alteraIdioma(minhasessionlang); } },

@oleg-am
Copy link

oleg-am commented Feb 9, 2017

"webpack": "^1.13.1",
"json-loader": "^0.5.4",

My solution:

  • add !json! to import path:
    import { version } from '!json!../../config/version.json';
  • add loader to WebPack preloaders:
module: {
          preLoaders: [
              {
                test: /\.json$/,
                loader: 'json-loader',
                include: '../../config/version.json',
              },
            ]
      }

@Valerz
Copy link

Valerz commented Mar 2, 2017

Thanks @oleg-am
Adding !json! to import path solved this for me

@vieillecam
Copy link

vieillecam commented Mar 13, 2017

Any update on this issue ?
I still having the problem, even with !json! or with json-loader in config file or even with pre-loader.
I'm always having : SyntaxError: Unexpected token � in JSON at position 0

@michael-ciniawsky
Copy link
Member

@vieillecam I'm not 💯 on this but if you're on webpack v2 I think json-loaderis obsolete (Core Integration) could you try without any JSON specific setup?

I need to wait for reply on the webpack slack regarding this, in case it shouldn't be solved 😛

@michael-ciniawsky michael-ciniawsky self-assigned this Mar 13, 2017
@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Mar 13, 2017

@vieillecam
Copy link

Thanks for you reply, it's already what I tried also without success. I'm not loading the loader explicitly and still receive special characters before the json loaded by webpack, as it was loaded already by something that put theses strange characters.

@Valerz
Copy link

Valerz commented Mar 14, 2017

I've seen this issue before with a json file having the diammond ? character. Some text editors could pick it up and some didn't. From memory sublime text did but atom didn't. Either try open the json in a different text editor and you should see it or copy the contents into a notepad and save that as the json.

@es6Test
Copy link
Author

es6Test commented Mar 14, 2017

unsubscribing from my own thread, doesn't bother me anymore with WP2

@michael-ciniawsky
Copy link
Member

@es6Test 👍

@michael-ciniawsky
Copy link
Member

@vieillecam See @pizdetss comment and if you still got regressions either open a new issue and a test repo for reproduction would be appreciated also 😛 , e.g does it happen with every JSON file or with specific ones generated somewhere etc...

@vieillecam
Copy link

@pizdetss Thanks for you comment, that was exactly my problem, I was doing my Json file only with Visual Studio Code and Sublime text and both of them were adding these extra ranges characters... Editing it with notepad++ resolve the issue. Thanks !

@sughanpandita
Copy link

"Module not found: Error: Can't resolve 'foo.json"
'use strict';

const webpack = require("webpack");
const path = require('path');

module.exports = {
entry: {
app: './build/aws.js',
libraries: ['./src/libraries.js']
},

resolve: {

modules: [
'node_modules',
path.resolve('./build'),
path.resolve('.'),
path.resolve('../../../lib')
]
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'libraries',
filename: 'lib.bundle.js'
})
],

output: {
path: './dist',
filename: 'bundle.js'
},
rules: [
{
test: /.json$/,
use: 'json-loader'
}
],
loaders:[

 { test: /vendor\/.+\.(jsx|js)$/,
     loader: 'imports?jQuery=jquery,$=jquery,this=>window',
 },

]
};

AWS.JSX
import foo from 'foo.json'

@sigeje
Copy link

sigeje commented Apr 10, 2017

I got the same problem.

"json-loader": "^0.5.4",
"webpack": "^1.13.3",

import text from './titles.json'

solved by adding './' before filename as per below

import text from './titles.json'

@ayslanf
Copy link

ayslanf commented Feb 8, 2018

I'd suggest you play a little bit with the paths like ./../../temp/sample.json for example, depending where your request comes from it doesn't resolve ./ as a root.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests