symfony2.3 configure assetic with less

2019-07-17 03:27发布

问题:

I am using Symfony "2.3.*" and I attempt to make assetic less filter working. I do all of this in the "dev" environment.

When I issue the config:dump-reference assetic I get the following output:

assetic:
    debug:                %kernel.debug%
    use_controller:
        enabled:              %kernel.debug%
        profiler:             false
    read_from:            %kernel.root_dir%/../web
    write_to:             %assetic.read_from%
    java:                 /usr/bin/java
    node:                 /usr/bin/node
    node_paths:           []
    ruby:                 /usr/bin/ruby
    sass:                 /usr/bin/sass
    variables:
        # Prototype
        name:                 []
    bundles:
        # Defaults:
        - FrameworkBundle
        - SecurityBundle
        - TwigBundle
        - MonologBundle
        - SwiftmailerBundle
        - AsseticBundle
        - DoctrineBundle
        - SensioFrameworkExtraBundle
        - FOSUserBundle
        - JMSSerializerBundle
        - YuccaPrerenderBundle
        - WebProfilerBundle
        - SensioDistributionBundle
        - SensioGeneratorBundle
    assets:
        # Prototype
        name:
            inputs:               []
            filters:              []
            options:

                # Prototype
                name:                 []
    filters:
        # Prototype
        name:                 []
    twig:
        functions:

            # Prototype
            name:                 []

So, my config looks like :

assetic:
    debug:          %kernel.debug%
    use_controller: false
    bundles:
      - PlastoriaGiftoFrontBundle
    #java: /usr/bin/java
    node: "/usr/bin/nodejs"
    node_paths: [ "/home/webadmin/websites/gifto/node_modules" ]
    filters:
        cssrewrite: ~
        less: ~
        closure:
            jar: %kernel.root_dir%/Resources/java/compiler.jar
        yui_css:
            jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.8.jar

To test that it works, I do open the Symfony Console and issue cache:clear Then I go to the cache dir (app/cache) and do rm -rf *. After I come back in Symfony console and launch these two commands:

assets:install
assetic:dump

And I am greeted with the following message :

Dumping all dev assets. Debug mode is on. 13:51:22 [file+] /home/webadmin/websites/gifto/app/../web/css/ac28136.css [Assetic\Exception\FilterException] An error occurred while running: '/usr/bin/nodejs' '/tmp/assetic_lessjHtxDR' Error Output: module.js:340 throw err; ^ Error: Cannot find module 'less' at Function.Module._resolveFilename (module.js:338:15) ... (truncated)

The configuration for the node binary is taken into account : An error occurred while running: '/usr/bin/nodejs'

By default, the value is /usr/bin/nodejs.

I can't figure out what's wrong. Did install "less" node module in the application using npm and when issuing npm ls, I can see it is properly installed.

I also launched nodejs in the application directory and did a require('less') and it worked without error.

What is the right way of configuring assetic with nodejs ?

回答1:

For mac users this is

node: "/usr/local/bin/node"
node_paths: [/usr/local/lib/node_modules/]


回答2:

Add

assetic:
   node_paths:           [/usr/lib/node_modules/]

in your config.yml file.

/usr/lib/node_modules/ works for Ubuntu.



回答3:

I was using Centos with a cPanel installation. I made the mistake of not realizing when you run npm install less that it would be in the directory I called that from, which in my case was my project root directory.

Whatever directory you were in when you installed less the node_modules folder should be there. This is the wrong location for it however.

Assuming you are in the same directory you ran the install from... Run this command to move it to the correct location

mv node_modules /usr/local/lib/node_modules

Then you need to add a line to your config.yml

assetic:
    debug:          '%kernel.debug%'
    use_controller: '%kernel.debug%'
    filters:
        cssrewrite: ~
        uglifyjs2:
            # the path to the uglifyjs executable
            bin: /usr/bin/uglifyjs
        uglifycss:
            bin: /usr/bin/uglifycss
        less: 
            node_paths: [/usr/local/lib/node_modules/]
            bin: /usr/bin/less

In my case I'm using a lot of other filters, but the key here is the node_paths: [/usr/local/lib/node_modules/], make sure you have that. Notice that is now set to a location that the script can access globally.

Now it will work. Hope this helps someone.



回答4:

Found,

I entered in the server as root (Yes, I know, bad practice) then I su webadmin!

The trick is to really login as webadmin or su webadmin -l.

I tested and it was the issue.