I started to use SystemJs. I don't find exact example about how to import a node module? How could I configure SystemJs and how to import this. Because after npm install import 'package-name' from 'package-name'; it could not find the module.
It would be great if somebody would write an example.
Thank you
Try something like:
import nodeModule from '#node/node-module-name' [1]
[1]
Related
I am new to EmberJS and I want to import and use the crypto-js in my EmberJS app, I used ember install crypto-js to install the package, then add those lines in ember-cli-build.js to add them to ember build to use SHA256 function:
app.import('node_modules/crypto-js/core.js');
app.import('node_modules/crypto-js/crypto-js.js');
app.import('node_modules/crypto-js/sha256.js');
And I can see that in browser, assets/node_modules has the crypto-js folder with above 3 files. However I still got Could not import module 'crypto-js' error. How to solve it? Thanks!
There is no need for app.import.
As long as you have ember-auto-import installed, or you are using embroider (which defers to webpack), you can follow the documentation: https://www.npmjs.com/package/crypto-js
import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';
Thanks for all the answers. It is an old project and after I installed ember-auto-import and webpack i got many build errors. I dont want to waste time on fixing them, but finally i got it worked:
app.import('node_modules/crypto-js/crypto-js.js',{
using:[
{transformation: 'amd', as: 'CryptoJS'}
]
});
Then I can import it and use in other .js files, like this:
import CryptoJS from 'CryptoJS';
Create a file called crypto-js.js in the shims folder and add this code
import 'crypto-js/core';
import 'crypto-js/sha256';
export default window.CryptoJS;
In your ember-cli-build.js file, add this linne:
app.import('vendor/shims/crypto-js.js', { using: [{ transformation: 'cjs', as: 'crypto-js' }] });
in your component, controller or model, you can import the library and use the SHA256 function like this
import crypto from 'crypto-js';
const hash = crypto.SHA256("Message to hash");
console.log(hash.toString());
I have two javascript libraries that I need to integrate into my React app. First I tried this:
import d3 from "https://d3js.org/d3.v4.min.js"
import techan from "http://techanjs.org/techan.min.js"
That produced error:
./src/components/CandlestickChart/CandlestickChart.jsx Module not
found: Can't resolve 'http://techanjs.org/techan.min.js' in
'C:\Users\Greg\Projects\react-demos\my-react-splitter-layout\src\components\CandlestickChart'
So I went to http://teckanjs.org/techan.min.js, copied the code, ran it through a beautifier, and saved it to techan.js, located in the same folder as CandlestickChart.jsx. I changed import to
import techan from "./techan.js"
Then I got similar error:
./src/components/CandlestickChart/CandlestickChart.jsx Module not
found: Can't resolve 'http://techanjs.org/techan.min.js' in
'C:\Users\Greg\Projects\react-demos\my-react-splitter-layout\src\components\CandlestickChart'
So I did the same thing as with the other one and changed the import to:
import d3 from "./d3.v4.js"
Then I got error:
./src/components/CandlestickChart/d3.v4.js Module not found: Can't
resolve './requirejs' in
'C:\Users\Greg\Projects\react-demos\my-react-splitter-layout\src\components\CandlestickChart'
So I found requirejs online and did the same thing as the others and added an import to d3.v4.js like this:
import requirejs from "./requirejs";
But still getting the same error. What's the trick to this?
Install it as a local package. Run the following:
npm install d3
Then you'll be able to import it in your JavaScript:
import * as d3 from "d3";
Note the "d3", not the "./d3" - the lack of the ./ indicates that you want to install from a module package, not from a file in the same directory.
Besides going through the documentation of the package and trial/error attempts, how can I know for sure if an npm packaged can be imported with the ES6 import syntax?
Is there a file inside the package folder that I can check?
I constantly question myself about this.
For example:
The md5 package DOCS only mentions md5 = require('md5'); but I've just tested and it works with import md5 from 'md5'
How can I inspect the package and know for sure?
Since you are using Babel and webpack, you can always use import. Webpack is taking care of transpiling the import statements in your code to include the code from the npm packages. I can't think of any case where import would not work while require would.
You must check if the module/package has a dist or a src folder. If the package has a dist folder, Then it can be imported using es6's import statement. Otherwise if it has a src folder, Then it can't be imported using es6's import statement and you must use cjs's require function to import such modules. If you try to import a cjs module using the import statement, You will get the following error:
Syntax Error: Cannot use import statement outside a module
I've started a project with the new vue-cli 3.0 and I've added the qwery npm module in node package.json
npm i qwery
and in my-file.js which is at same level as main.js I import it the following way:
import {qwery as $q} from "qwery"
The build goes ok however in the browser $q is undefined and webpack has imported it as qwery__WEBPACK_IMPORTED_MODULE_8__.
Clearly I'm not doing it the right way can somebody give me a hint what I'm doing wrong?
You need to import it like this
import $q from 'qwery';
I've been trying to import the sjcl library, which has many files, but I'm only able to import 1 file from it.
From command line:
npm install sjcl --save
react-native link
In a RN JS file:
import sjcl from 'sjcl';
Looks like this doesn't import everything in the sjcl node package, it only imports file sjcl.js from node_modules/sjcl/. I also need sha1.js from node_modules/sjcl/core/sha1.js. I've tried various ways of importing it, but nothing works.
How can I import an entire npm library into a React Native project?
I had the same problem trying to load the ECC module, here is how I worked around it (I installed sjcl using npm). Create a file LocalExports.js in which put this
import sjcl from 'sjcl';
export {default as sjcl} from 'sjcl';
window.sjcl = sjcl;
then import like this in any other file
import {sjcl} from './LocalExports';
import 'sjcl/core/bn';
import 'sjcl/core/ecc';
*edit - I just found this link, which is the official way to do this
https://github.com/bitwiseshiftleft/sjcl/wiki/Getting-Started