https://pulumi.com logo
Title
s

stocky-island-3676

01/08/2020, 4:04 PM
I get the error
import * as gcp from '@pulumi/gcp'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
from a
dependency
saved in Github repo. What am I missing?
b

broad-helmet-79436

01/08/2020, 4:09 PM
looks like you’re using code that’s not been transpiled
import
isn’t valid in node unless the file is an
.mjs
file and you’re in node 13 or newer
are you importing the wrong file, maybe?
which dependency is it?
s

stocky-island-3676

01/08/2020, 4:12 PM
It’s simply one Typescript file (our own, private module). Yes, the file transpiled to Javascript is
.gitignore
d, i.e. not in the Github repo. That could explain why it works locally. You know what the best-practice is?
b

broad-helmet-79436

01/08/2020, 4:15 PM
it might still be included in the published package even if it’s ignored 🙂
you can’t import a typescript module unless you transpile things inside node_modules, and that usually makes your build extremely slow
so you want to transpile the module when you publish it and change your package.json
"main"
field to point to the transpiled code
typically
build/index.js
or something
s

stocky-island-3676

01/08/2020, 4:23 PM
I see. The first (transpiling on each build) would require this in
package.json
, or?
"scripts": {
        "build": "tsc"
    },
(taken from https://github.com/pulumi/pulumi-gcp/blob/master/sdk/nodejs/package.json#L12-L14) The other way would be to push also the transpiled JS file to the Github repo.
Hmm, the
"scripts": {
    "build": "tsc",
    "prepare": "npm run build"
  },
doesn’t work, either. (The
prepare
part is from https://www.reddit.com/r/typescript/comments/9ctsbd/how_do_i_publish_a_typescript_library_to_a/e5db0e4/)
b

broad-helmet-79436

01/08/2020, 6:38 PM
sorry, I had to go do other stuff
how do you reference the dependency? is it published on an internal npm server? or are you working with a monorepo type deal?
s

stocky-island-3676

01/09/2020, 12:49 PM
@broad-helmet-79436 No worries. Thanks for your help so far. I want to avoid to use a (private) NPM server (no matter the official or an internal server). I.e. to reference the dependency via
git+https://
. Seems like pushing the transpiled JavaScript code is the way to go, as you mentioned. Others take the same approach: https://stackoverflow.com/questions/55002806/how-to-publish-a-private-typescript-npm-package-in-git/55004794#55004794
👍 1