Understanding r.js, almonds and relative paths

I see this answer , but AFAICT this does not work for me. Maybe I'm doing something stupid.

I use almond and grunt-contrib-requirejs . I tried a bunch of things

Here is my layout

.
├── Gruntfile.js
├── 3rdparty
│   ├── require.js
├── src
│   ├── lib.js
│   └── main.js
└── node_modules
    └── almond
        └── almond.js

And here is my grunt-contrib-requirejs configuration

requirejs: {
  full: {
    options: {
      baseUrl: "./",
      name: "node_modules/almond/almond.js",
      include: [ "src/main.js" ],
      out: "dist/app.js",
      optimize: "none",
    },
  },
},

main.js is as follows

requirejs(['./lib',], function(lib) {
  lib.hello();
});

lib.js is as follows

define([], function() {
    return {
      hello: function() {
          console.log("hello from lib");
      },
    };
});

If you run a page that uses require.js, as in

<script src="3rdparty/require.js" data-main="src/main.js"></script>

It works great. You can see how he lives here . Check the console and you will see that it printshello from lib

So I'm running. Then I launch the page that uses dist/app.jsand I get an error

Uncaught Error: undefined missing lib

Here is a live page .

dist/app.js lib

define('src/lib',[], function() {
   ...
});

requirejs(['./lib'], function(lib) {
  ...
});

, , r.js src/lib, , main ./lib.

r.js. " ".

?

, , baseUrl ./src

requirejs: {
  full: {
    options: {
      baseUrl: "./src",
      name: "node_modules/almond/almond.js",
      include: [ "src/main.js" ],
      out: "dist/app.js",
      optimize: "none",
    },
  },
},

{ [Error: Error: ENOENT: no such file or directory, open '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js'
    at Error (native)
]
  originalError: 
   { [Error: ENOENT: no such file or directory, open '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js']
     errno: -2,
     code: 'ENOENT',
     syscall: 'open',
     path: '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js',
     fileName: '/Users/gregg/temp/grunt-contrib-requirejs-example/src/node_modules/almond/almond.js' } }

,

requirejs: {
  full: {
    options: {
      baseUrl: "./src",
      name: "../node_modules/almond/almond.js",
      include: "main",
      out: "dist/app.js",
      optimize: "none",
    },
  },
},

{ [Error: Error: ERROR: module path does not exist: ../node_modules/almond/almond.js for module named: ../node_modules/almond/almond.js. Path is relative to: /Users/gregg/temp/grunt-contrib-requirejs-example
    at /Users/gregg/temp/grunt-contrib-requirejs-example/node_modules/requirejs/bin/r.js:30214:35
]
  originalError: [Error: ERROR: module path does not exist: ../node_modules/almond/almond.js for module named: ../node_modules/almond/almond.js. Path is relative to: /Users/gregg/temp/grunt-contrib-requirejs-example] }

?

github , .

+4
1

, .

r.js ,

requirejs: {
  full: {
    options: {
      baseUrl: "./src",
      paths: {
        almond: "../node_modules/almond/almond",
      }
      name: "almond",
      include: [ "main.js" ],
      out: "dist/app.js",
      optimize: "none",
    },
  },
},
+2

Source: https://habr.com/ru/post/1628690/


All Articles