Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acorn throwing an error on a perfectly valid package.json file (?) #415

Closed
nfriedly opened this issue May 20, 2016 · 4 comments
Closed

Acorn throwing an error on a perfectly valid package.json file (?) #415

nfriedly opened this issue May 20, 2016 · 4 comments

Comments

@nfriedly
Copy link

nfriedly commented May 20, 2016

Hey, I'm not sure what's going on here of if this is even really an acorn issue, but I'm getting this logged in my build output when trying to build a bundle via webpack:

ERROR in ./~/websocket/package.json
Module parse failed: /Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/websocket/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:9)
    at Parser.pp.raise (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:920:13)
    at Parser.pp.unexpected (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1483:8)
    at Parser.pp.semicolon (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1462:73)
    at Parser.pp.parseExpressionStatement (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1976:8)
    at Parser.pp.parseStatement (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1754:188)
    at Parser.pp.parseBlock (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1991:21)
    at Parser.pp.parseStatement (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1735:19)
    at Parser.pp.parseTopLevel (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1648:21)
    at Parser.parse (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:1616:17)
    at Object.parse (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/acorn/dist/acorn.js:882:44)
    at Parser.parse (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/Parser.js:902:15)
    at DependenciesBlock.<anonymous> (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/NormalModule.js:104:16)
    at DependenciesBlock.onModuleBuild (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
    at nextLoader (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
    at /Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
    at Storage.provide (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:52:20)
    at CachedInputFileSystem.readFile (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:140:24)
    at DependenciesBlock.onLoadPitchDone (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack-core/lib/NormalModuleMixin.js:255:7)
    at DependenciesBlock.loadPitch (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack-core/lib/NormalModuleMixin.js:182:27)
    at DependenciesBlock.doBuild (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack-core/lib/NormalModuleMixin.js:241:4)
    at DependenciesBlock.build (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/NormalModule.js:84:14)
    at Compilation.buildModule (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/Compilation.js:126:9)
    at /Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/Compilation.js:309:10
    at /Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/NormalModuleFactory.js:58:13
    at NormalModuleFactory.applyPluginsAsyncWaterfall (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/tapable/lib/Tapable.js:75:69)
    at onDoneResolving (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/NormalModuleFactory.js:38:11)
    at onDoneResolving (/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/NormalModuleFactory.js:121:6)
    at /Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/webpack/lib/NormalModuleFactory.js:116:7
    at /Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/async/lib/async.js:726:13
    at /Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/async/lib/async.js:52:16
 @ ./~/websocket/lib/version.js 1:17-43

For reference, here's the package.json file that it's claiming is invalid:

{
  "_args": [
    [
      "websocket@^1.0.22",
      "/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/watson-speech"
    ]
  ],
  "_from": "websocket@>=1.0.22 <2.0.0",
  "_id": "websocket@1.0.23",
  "_inCache": true,
  "_installable": true,
  "_location": "/websocket",
  "_nodeVersion": "0.10.45",
  "_npmOperationalInternal": {
    "host": "packages-16-east.internal.npmjs.com",
    "tmp": "tmp/websocket-1.0.23.tgz_1463625793005_0.4532310354989022"
  },
  "_npmUser": {
    "email": "brian@worlize.com",
    "name": "theturtle32"
  },
  "_npmVersion": "2.15.1",
  "_phantomChildren": {},
  "_requested": {
    "name": "websocket",
    "raw": "websocket@^1.0.22",
    "rawSpec": "^1.0.22",
    "scope": null,
    "spec": ">=1.0.22 <2.0.0",
    "type": "range"
  },
  "_requiredBy": [
    "/watson-speech"
  ],
  "_resolved": "https://registry.npmjs.org/websocket/-/websocket-1.0.23.tgz",
  "_shasum": "20de8ec4a7126b09465578cd5dbb29a9c296aac6",
  "_shrinkwrap": null,
  "_spec": "websocket@^1.0.22",
  "_where": "/Users/nfriedly/Downloads/cognitive-ads-ui/node_modules/watson-speech",
  "author": {
    "email": "brian@worlize.com",
    "name": "Brian McKelvey",
    "url": "https://www.worlize.com/"
  },
  "browser": "lib/browser.js",
  "bugs": {
    "url": "https://github.com/theturtle32/WebSocket-Node/issues"
  },
  "config": {
    "verbose": false
  },
  "contributors": [
    {
      "email": "ibc@aliax.net",
      "name": "Iñaki Baz Castillo",
      "url": "http://dev.sipdoc.net"
    }
  ],
  "dependencies": {
    "debug": "^2.2.0",
    "nan": "^2.3.3",
    "typedarray-to-buffer": "^3.1.2",
    "yaeti": "^0.0.4"
  },
  "description": "Websocket Client & Server Library implementing the WebSocket protocol as specified in RFC 6455.",
  "devDependencies": {
    "buffer-equal": "^0.0.1",
    "faucet": "^0.0.1",
    "gulp": "git+https://github.com/gulpjs/gulp.git#4.0",
    "gulp-jshint": "^1.11.2",
    "jshint-stylish": "^1.0.2",
    "tape": "^4.0.1"
  },
  "directories": {
    "lib": "./lib"
  },
  "dist": {
    "shasum": "20de8ec4a7126b09465578cd5dbb29a9c296aac6",
    "tarball": "https://registry.npmjs.org/websocket/-/websocket-1.0.23.tgz"
  },
  "engines": {
    "node": ">=0.8.0"
  },
  "gitHead": "ba2fa7e9676c456bcfb12ad160655319af66faed",
  "homepage": "https://github.com/theturtle32/WebSocket-Node",
  "keywords": [
    "websocket",
    "websockets",
    "socket",
    "networking",
    "comet",
    "push",
    "RFC-6455",
    "realtime",
    "server",
    "client"
  ],
  "license": "Apache-2.0",
  "main": "index",
  "maintainers": [
    {
      "email": "brian@worlize.com",
      "name": "theturtle32"
    }
  ],
  "name": "websocket",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/theturtle32/WebSocket-Node.git"
  },
  "scripts": {
    "gulp": "gulp",
    "install": "(node-gyp rebuild 2> builderror.log) || (exit 0)",
    "test": "faucet test/unit"
  },
  "version": "1.0.23"
}

As far as I can tell, everything in that file is perfectly valid (node.js parses it without complaint). I think 2:9 is the " at the end of "_args": which seams to be in order to me.

This could be related to webpack/webpack#303

@RReverser
Copy link
Member

acorn is a JavaScript parser, not JSON parser. For JSON, you should use just JSON.parse.

@nfriedly nfriedly changed the title Acorn throwing and error on a perfectly valid package.json file (?) Acorn throwing an error on a perfectly valid package.json file (?) May 20, 2016
@nfriedly
Copy link
Author

Ok, fair enough. Not sure I understand the failure though - isn't all JSON also valid JavaScript?

@nfriedly
Copy link
Author

(Or, at the very least, could Acorn throw a more clear error message for this kind of problem?)

@RReverser
Copy link
Member

RReverser commented May 20, 2016

Well, the problem is that beginning is totally valid JavaScript, but not with a meaning you might be putting into it. It's very similar to confusing issue people are getting when using arrow functions as () => { x: 1 } - such syntax is still a valid JavaScript, but it has a meaning of a block with a label x which points to an expression statement with 1 as an expression, and perfectly parses & executes, just doesn't return anything. Same for JSON - Acorn does attempt to parse it as a valid JavaScript according to JavaScript semantics in statement context, and fails only when syntax is really invalid.

If you want to still parse this as a JavaScript expression, you might want to look at parseExpressionAt(input, offset, options) method which is described in the docs, but note that it still returns an ESTree AST, not the object itself as JSON.parse does because it needs to support all the JavaScript semantics.

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

No branches or pull requests

2 participants