webpack: SyntaxError: Unexpected token (package.json) with Webpack

ERROR in Entry module not found: SyntaxError: D:\ProjectPath\package.json (directory description file): SyntaxError: Unexpected token

webpack.config.js

module.exports = {
    context: __dirname,
    entry: 'main.ts',
    output: {
        path: __dirname + '/Builds/js',
        filename: 'all.js'
    },
    resolve: {
        extensions: ['', '.js', '.ts']
    },
    module: {
        loaders: [
          {
              test: /\.ts$/,
              loader: 'ts'
          },
          {
              test: /\.json$/,
              loader: "json"
          }
        ]
    }
};

package.json

{
    "version": "1.0.0",
    "name": "RevenueTracker",
    "description": "A angular 2 app",    
    "scripts": {
        "build": "webpack",
        "typings": "typings",
        "preinstall": "npm i -g bower",
        "postinstall": "typings install"
    },
    "dependencies": {
        "jquery": "~1.11.3",
        "@angular/common": "2.0.0-rc.1",
        "@angular/compiler": "2.0.0-rc.1",
        "@angular/core": "2.0.0-rc.1",
        "@angular/http": "2.0.0-rc.1",
        "@angular/platform-browser": "2.0.0-rc.1",
        "@angular/platform-browser-dynamic": "2.0.0-rc.1",
        "@angular/router": "2.0.0-rc.1",
        "@angular/router-deprecated": "2.0.0-rc.1",
        "@angular/upgrade": "2.0.0-rc.1",
        "systemjs": "0.19.26",
        "es6-shim": "^0.35.0",
        "reflect-metadata": "0.1.2",
        "rxjs": "5.0.0-beta.6",
        "zone.js": "^0.6.12",
        "highcharts": "^4.2.3",
        "ng2-highcharts": "^0.3.4",
        "moment": "^2.13.0"
    },
    "devDependencies": {
        "typescript": "^1.8.0",
        "typings": "^0.7.12",
        "gulp": "3.9.0",
        "gulp-bower": "^0.0.13",
        "gulp-uglify": "1.5.1",
        "gulp-concat": "2.6.0",
        "gulp-sourcemaps": "1.6.0",
        "require-dir": "0.3.0",
        "webpack": "^1.12.14",
        "webpack-merge": "^0.9.0",
        "file-loader": "^0.8.5",
        "ts-loader": "^0.8.1",
        "json-loader": "0.5.4"
    }
}

I don’t find any syntax error in package.json I am trying from a long time but the same error exist. You will notice i have given absolute path for the entry as

entry: ‘main.ts’,

when i give relative path (entry: ‘./main’) it gives error ERROR in Entry module not found: Error: Cannot resolve ‘file’ or ‘directory’ ./main in D:\ProjectPath

Then it resolves the path for main.ts and finally gives the error as mentioned above. ERROR in Entry module not found: SyntaxError: D:\ProjectPath\package.json (directory description file): SyntaxError: Unexpected token

This is very strange behavior. Please let me know if anybody found same issue and how did u resolve?

Thank you.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

@blackcloud2333 try to:

  1. copy content of package.json (skip the first line since from the error message I would say there is some special character hidden in the beginning of the file)
  2. remove package.json
  3. create new package.json file and paste content (retype first line)

Having same issue when using yarn instead of npm.

Thanks. I just made a temporary fix by changing Unicode(UTF-8 with signature) - Codepage 65001 encoding to Western European(Windows) with the Advanced save options available in visual studio. Its working fine now.

Are we working on this for permanent solution ?

The package file ? The file is saved with Unicode(UTF-8 with signature) - Codepage 65001 encoding.

The strange thing is when package.json file present in the directory its giving different error.

ERROR in Entry module not found: Error: Cannot resolve ‘file’ or ‘directory’ ./main in D:\ProjectPath

But this same configuration is working fine when i remove the package.json file from the directory.

Can you please suggest ?

In my case it was a extra trailing comma after the last json entry where a valid .json doesn’t expect one. image

It happened to me when I was pasting some package defs from web into package.json. Changing encoding to UTF-8 solved issue.

UTF-8 with signature

That’s a BOM. We need to strip the DOM when loading the file while resolving…