2017-08-14 124 views
0

我嘗試使用下面的鏈接導入js文件

https://blog.risingstack.com/node-js-at-scale-module-system-commonjs-require/

我收到錯誤文件未找到錯誤爲什麼實現CommonJS的彭定康,而爲什麼需要需要擴展名爲.js? 這裏是我的代碼 https://plnkr.co/edit/mYeCYw9MOr87b4dQZr5I?p=preview 錯誤出現在該行 const add = require('./login')

當我login它的工作原理後添加的.js但在例如它不添加.js擴展爲什麼呢?大部分時間我用擴展名需要js

const add = require('./login') 

console.log(add(4, 5)) 
+0

所以你的母鹿使用'.js'擴展名。爲什麼不繼續呢? – Justinas

+0

順便說一下,R​​equireJS,CommonJS和SystemJS是不同的框架。 – lilezek

回答

1

如果你想繼續能夠省略.js擴展,你可以用你的SystemJS配置defaultExtension選項。如果我添加到您的plunker,它加載而不會出現錯誤:

SystemJS.config({ 
    packages: { 
    '': { 
     defaultExtension: "js" 
    } 
    } 
}) 

這個定義包含了所有你的代碼的包和其默認的擴展名,如果不是在模塊請求規定,是.js