Webpack-encore: [Support] Cannot determine webpack context. (Are you executing webpack from a directory outside of your project?).

Created on 6 Jul 2017  路  5Comments  路  Source: symfony/webpack-encore

I've just installed Encore to test it and start using it in my projects (great great work!!! Really, thank you!).

I'm immediately having an issue.

When trying to run ./node_modules/.bin/encore dev I receive the exception:

Cannot determine webpack context. (Are you executing webpack from a directory outside of your project?). Try passing the --context option.

I've simply followed the instructions in the official documentation about the first example and the result is this exception.

My project is structured like so:

- app
- bin
- nome_modules
- src
- tests
- var
- vendor
- web
...
composer.json
composer.lock
webpack.config.js

So, basically, I have the classical structure of a Symfony 3.3 project, plus some folders related to JS.

Can anyone help me solve this problem?

This is the very first time I approach a more sophisticated solution to manage css and javascript assets.

Unitl now I've done all putting those files in src/AppBundle/Resources/public/ so I'm not very experienced with this new flow.

Thank you!

I've opened also a question on StackOverflow.

Most helpful comment

But I've used npm instead of yarn: maybe this is the reason why the package.json were not created?

Yep, that's likely the problem :). I'd recommend using yarn, especially if you're starting new with this stuff (so that you will not have any surprises). But if you want to use npm, you should:

  • Run (at the root of your project) npm init. This will generate a basic package.json for you. The questions it asks are generally not important - you can just use the default value for most of them.

  • Re-run all the npm install commands that you used to install Encore, but make sure you add a --save-dev flag on the end. Unless you're using the latest version of npm (npm 5), you must add --save-dev when you install things with npm, otherwise it will install them but NOT update the package.json file, which is a bit silly :).

Cheers!

All 5 comments

Hey @Aerendir!

Awesome, welcome :). Does your project have a package.json file? It's not mentioned in your file list, and we actually look for this to find the root of your project. That's the first place to look. We might need to improve the error message if that's the case. Though, if it is, how did you install everything and avoid creating the package.json file?

Cheers!

Hi @weaverryan , thank you :)

Anyway, no, currently there is no package.json file.

Looking at it, it there isn't.

I've not created it as the documentation states that it should be created automatically:

This command creates (or modifies) a package.json file and downloads dependencies into a node_modules/ directory.

But I've used npm instead of yarn: maybe this is the reason why the package.json were not created?

Anyway, what should have I to put into it if I want to create it manually?

But I've used npm instead of yarn: maybe this is the reason why the package.json were not created?

Yep, that's likely the problem :). I'd recommend using yarn, especially if you're starting new with this stuff (so that you will not have any surprises). But if you want to use npm, you should:

  • Run (at the root of your project) npm init. This will generate a basic package.json for you. The questions it asks are generally not important - you can just use the default value for most of them.

  • Re-run all the npm install commands that you used to install Encore, but make sure you add a --save-dev flag on the end. Unless you're using the latest version of npm (npm 5), you must add --save-dev when you install things with npm, otherwise it will install them but NOT update the package.json file, which is a bit silly :).

Cheers!

Ok, I've created the package.json file and now all works well.

But the documentation should be fixed as now it tells that the file is created automatically while it isn't.

Also the exception could be more clear...

In my package.json I've put this:

{
    "version": "0.0.1",
    "name": "My-App",
    "private": true,
    "devDependencies": {
        "@symfony/webpack-encore": "~0.9",
        "bootstrap": "~3"
    }
}

And just for completeness, after running npm init:

{
  "name": "my-app",
  "version": "0.0.1",
  "description": "My app description",
  "main": "webpack.config.js",
  "directories": {
    "doc": "docs",
    "test": "tests"
  },
  "dependencies": {
    "bootstrap": "^3.3.7"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://[email protected]/Aerendir/my-app.git"
  },
  "author": "Aerendir",
  "license": "UNLICENSED",
  "homepage": "https://bitbucket.org/Aerendir/my-app#readme"
}

Again, really thank thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BackEndTea picture BackEndTea  路  3Comments

powerlimit picture powerlimit  路  4Comments

pensiero picture pensiero  路  4Comments

zek0faws picture zek0faws  路  4Comments

JohnnyEvo picture JohnnyEvo  路  3Comments