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

'Main is required': Local package.json clobbers packaged file on OSX. #1503

Closed
shadowmint opened this issue Jan 23, 2014 · 21 comments
Closed
Assignees
Labels
Milestone

Comments

@shadowmint
Copy link

There are a few issues like #388 #831 where on OSX people seem to have difficulty running apps with valid package structure and/or package.json files. The general advice is that: you probably zipped it up wrong, like this:

zip -r app.nw app/

Instead of:

cd app
zip -r ../app.nw .

However, there is actually a bug here.

If you have a local file called package.json, it clobbers the value inside the zip file.

Example of layout:

doug:dist doug$ du -ah
4.0K    ./all.nw
  0B    ./Gruntfile.js
4.0K    ./package.json
4.0K    ./src/index.html
4.0K    ./src/package.json
8.0K    ./src
 16K    .

And in all.nw:

doug:dist doug$ unzip -vl all.nw
Archive:  all.nw
 Length   Method    Size  Ratio   Date   Time   CRC-32    Name
--------  ------  ------- -----   ----   ----   ------    ----
 316  Defl:N      154  51%  01-23-14 15:21  f2887a4d  index.html
 141  Defl:N      105  26%  01-23-14 15:26  ee7399ca  package.json
--------          -------  ---                            -------
 457              259  43%                            2 files

Running results in:

../node.app/Contents/MacOS/node-webkit all.nw
Invalid package.json 
Field 'main' is required.

But it is valid:

doug:dist doug$ cat src/package.json
{
"main": "index.html",
"version": 1.0,
"name": "hello",
"window": {
"toolbar": false,
"width": 800,
"height": 600
}
}

The issue here is that the webkit working is incorrectly loading the wrong package.json file. The package.json in the current directory is being located instead. Indeed., removing the package.json file make it work perfectly.

This issue seems to be somehow related to the relative path of the node-webkit.app bundle and the source file.

Consider this directory structure:

4.0K    ./junk/junk2/all.nw
4.0K    ./junk/junk2
4.0K    ./junk
 69M    ./node.app
4.0K    ./package.json

Running:

../../node.app/Contents/MacOS/node-webkit all.nw

Fails, while this works perfectly:

doug:junk2 doug$ rm ../../package.json
doug:junk2 doug$ ../../node.app/Contents/MacOS/node-webkit all.nw

However, this also works perfectly:

doug:junk2 doug$ mv ../../package.json ..
doug:junk2 doug$ ../../node.app/Contents/MacOS/node-webkit all.nw

So, it's got something to do with having a package.json in the same folder as the .nw file, or in the same folder as the app bundle.

Either way, very weird behaviour.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@shadowmint
Copy link
Author

I'd venture to guess this turns up now and then and then disappears again because using a package.json to install npm packages it pretty common, but if you move your files around it 'suddenly works'.

@commadelimited
Copy link

I just updated NW today vs a version I installed a few weeks ago and now I'm getting this error when I run nodewebkit app:

Field 'main' is required.

Didn't do anything to the project other than reinstall some of the libs. Now the app won't run. Here's my directory tree, from the root of the project:

├── Gruntfile.js
├── app
│   ├── css
│   │   ├── main.css
│   ├── index.html
│   ├── js
│   │   ├── application.js
│   └── package.json
├── bower.json
├── package.json

As you can see I have 2 package.json files, one for installing node modules at the project level, the other is the manifest for the application itself. The package.json file contained within the app directory contains the following:

{
    "name": "directory",
    "main": "index.html",
    "version": "0.0.1",
    "single-instance": true,
    "window": {
        //"toolbar": false,
        "title": "Directory",
        "width": 850,
        "height": 600,
        "min_width": 850,
        "min_height": 600
    },
    "chromium-args": "--child-clean-exit"
}

The package.json file in the root does not have a main property, but even when I attempt to add one (pointing to app/index.html) I get the same error.

@commadelimited
Copy link

Also, here's the package.json for the root of the project:

{
  "name": "directory",
  "version": "0.0.0",
  "main": "app/index.html",
  "description": "",
  "keywords": [],
  "dependencies": {
    "xls-to-json": "~0.2.0"
  },
  "devDependencies": {
    "matchdep": "~0.1.2",
    "grunt": "~0.4.1",
    "grunt-contrib-copy": "~0.4.1",
    "grunt-contrib-sass": "~0.4.1",
    "grunt-contrib-concat": "~0.1.3",
    "grunt-contrib-jshint": "~0.4.3",
    "grunt-contrib-clean": "~0.4.1",
    "grunt-contrib-rename": "0.0.3",
    "grunt-contrib-compress": "~0.5.3",
    "xls-to-json": "~0.2.0"
  },
  "engines": {
    "node": ">=0.8.0"
  }
}

@paulociecomp
Copy link

same problem.

@commadelimited
Copy link

FYI, here are the versions for the libs I'm using on the computer for which this fails:

node-webkit: v0.8.5
node.js: v0.10.22
Chromium: 30.0.1599.66
bower: 1.3.1
yeoman: 1.1.2

To compare, my home machine can run the app just fine, even after rebasing my most recent changes and it has the following versions:

node-webkit v0.8.5
node.js v0.10.22
Chromium 30.0.1599.66
bower: 1.2.8
yeoman: 1.1.2

@commadelimited
Copy link

@rogerwang Do you have any input on this issue? Would love some guidance please.

@shadowmint
Copy link
Author

@commadelimited For what it's worth, I resolved this myself by bundling all the project assets into the distributable osx application folder (Content/Resources) in the node-webkit.app as a build step.

(the code may be slightly difficult to follow, but the details are here https://github.com/shadowmint/ruffx/blob/master/src/ruffx/webkit/__init__.py --> basically, every build, create a new copy of node-webkit.app, a new zip file of the app/ folder, and copy the zip file into node-webkit.zip/Contents/Resources)

You can then launch it via: open build/my.app

...but it is a slow and frustrating way to interact with node-webkit. I ended up doing dev on a windows machine and then just building on my mac. Not awesome.

@pablasso
Copy link

pablasso commented Apr 3, 2014

The same happens for me.

@commadelimited
Copy link

@shadowmint Unfortunately I don't have access to a Windows machine. I wonder if I should just removed all of the libs I'm using and reinstall. Not a huge deal, but I'd rather find out why this is happening when the only thing that appears different is the bower versions.

@rogerwang
Copy link
Member

I'll fix this in the next version. Thanks.

@rogerwang rogerwang added this to the 0.9.3 milestone Apr 3, 2014
@rogerwang rogerwang assigned rogerwang and gitchs and unassigned rogerwang Apr 3, 2014
@commadelimited
Copy link

That's awesome @rogerwang, thank you. Do you have an eta on the next version? Is there anything I can do now to work around this problem?

@commadelimited
Copy link

@rogerwang FYI I also have an open thread on the Google Groups forum for this issue:

https://groups.google.com/forum/#!topic/node-webkit/a6nFAx_U4Dc

I've had several people respond with suggestions and potential causes. Still doesn't appear to be a definitive solution though.

@commadelimited
Copy link

User @mauvm discovered on the node-webkit-boilerplate issues page that renaming package.json in the main nodewebkit node_modules directory allows the app to start working again.

I can confirm that renaming/moving /usr/local/lib/node_modules/nodewebkit/package.json allows my app to start running again.

@shama
Copy link

shama commented Apr 7, 2014

Just let me know if there is anything I need to fix in https://github.com/shama/nodewebkit Thanks!

@rawtaz
Copy link

rawtaz commented Apr 7, 2014

Let me ask a really stupid question.. "Renaming/moving /usr/local/lib/node_modules/nodewebkit/package.json" implies that one rename or remove it from something to something, but nowhere in that quote is there any hint about from or to what it should be renamed/moved. Can someone clarify, from what or to what should /usr/local/lib/node_modules/nodewebkit/package.json be renamed/moved?

@shadowmint
Copy link
Author

@shama There's a bug in https://github.com/rogerwang/node-webkit/blob/master/src/nw_package.cc#L256 which resolves to the wrong package.json in various circumstances as far as I can tell, but why it does that I'm not really sure, but I'm willing to get it's directly related to https://github.com/rogerwang/node-webkit/blob/master/src/nw_package.cc#L137

ie. The 3 level DirName() call is used in OSX to make it work in an app bundle, but when the code is not in an app bundle, this does weird things.

@mauvm
Copy link

mauvm commented Apr 8, 2014

@rawtaz For me simply renaming it to _package.json did the job.

@commadelimited
Copy link

That's exactly what I did. I'm concerned though that when in ready to package there might be unknown impacts.

On Apr 8, 2014, at 4:59 AM, Maurits van Mastrigt notifications@github.com wrote:

@rawtaz For me simply renaming it to _package.json did the job.


Reply to this email directly or view it on GitHub.

@girvo
Copy link

girvo commented Apr 8, 2014

Cheers @mauvm that hack worked on my Lion-running Mac. Weird little bug this one, it loads most of the package.json in my project directory, but main gets clobbered it seems.

My nw was installed using npm.

@gitchs
Copy link
Contributor

gitchs commented May 6, 2014

@rogerwang OSX's package.json search strategy lead to this problem.

  1. node-webkit will first look for node-webkit.app/Contents/Resources/app.nw, if file exists,it will try to unzip and use its package.json.
  2. Then node-webkit will try if there is a package.json at the same level of node-webkit.app,if file exists, it will use this file and ignore others.
  3. If both first and second strategy failed, it will search package.nw at the same level of node-webkit.app, if file exists node-webkit will try to unzip and use its package.json.
  4. Finally, node-webkit will try our command line argument.
  5. If all above steps fail, node-webkit will pop an error message.

So I think update this file will fix the bugs.I will have a try tomorrow.

@nwjs-bot
Copy link

This should be working with latest version now.

In 0.13 we changed to an optimized architecture so more features can be supported, see http://nwjs.io/blog/whats-new-in-0.13/ and it's good for keeping up with Chromium upstream -- we released with Node.js v6.0 and new Chromium versions within 1 day after upstream release.

The new version would fixed many issues reported here and we're scrubbing them. This issue is closed as we believe it should be fixed. Please leave a message if it isn't and we'll reopen it.

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

No branches or pull requests